473,378 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

why is my code giving a duplicate loops and segmentation fault..

can someone please look at this code and decipher it for me....i've
been stressed by it!
=============================================
//disk.cpp

#include "disk.h"
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <fstream>

using namespace std;

bool Cdisk::open(const char* filename, std::ifstream& file_in)
{
file_in.open(filename, ios::binary);
if (!file_in)
{
printf("Fail to Open\n");
return false;
}//if
return true;

}//open

bool Cdisk::eof(ifstream& file_in) const
{
if (file_in.eof())
return 1;
return 0;
}//eof

unsigned char Cdisk::read(unsigned char* buffer, std::ifstream&
file_in, int& current_buff_size, int& file_index)
{
if(current_buff_size <= 0)
{
file_index = 0;
file_in.read((char*)buffer, 512);
current_buff_size =file_in.gcount();
}

else if(current_buff_size==0)
{
exit(0); //terminates the whole program!!
}
current_buff_size--;
file_index++;

return (buffer[file_index]);
}//read

int Cdisk::seek(int position, std::ifstream& file_in, Cdisk& disk)
{
int start(0), end(0);
start =disk.tell(file_in);
file_in.seekg(0, ios::end);
end =disk.tell(file_in);
int size_of_file = end - start;
file_in.seekg(0, ios::beg);

return size_of_file;

/*
long pos;
long file_beg, file_end;
long file_size;
pos = fseek(file_in, position, 0);
if (pos == 0 || pos <= 511)
return true;
return false;*/
}//seek

int Cdisk::tell(std::ifstream& file_in) const
{
int start = file_in.tellg();

return start;
}//tell

void Cdisk::close(std::ifstream& file_in)
{
file_in.close();
}//close

=========================

#include <iostream // use this to write to the console
#include <string // you may or may not need this...
#include "disk.h" // see disk.h for a Cdisk class template and
comments
#include "disk.cpp"
#define length 512;

using namespace std;

// Write any functions or function prototypes you need here.
// Note that you may NOT call any Linux library functions to open,
read,
// close or position the disk file -- use your Cdisk class for that.
void output(unsigned char, int, bool&);
unsigned char retrieve_bytes(unsigned char* , ifstream& , int& ,
int& , Cdisk& );

int main(int argc, const char **argv)
{
// See if there's a "filename" parameter passed to this function
// NOTE: argv[0] is the full path name of this program
// argv[1] is the first parameter, argv[2] the second, etc.
// argc is the number of parameters passed, plus 1
std::ifstream file_in;
long Size_of_file(0), start(0), finish(0), address(0);
int remainder_bytes(0), position(0);
int file_index(0), sector_index(0), current_buff_size(0);
Cdisk disk;
unsigned char buffer[512];

if (argc != 2) {
cout << "No filename parameter" << endl;
return -1;
}

const char *filename= argv[1]; // this should be the filename
// write your hex dump program here -- it MUST use your Cdisk method
// functions to open, read, seek and close the binary file, for
example,
// bool opened= disk.open(filename); (returns true or false)

if(!disk.open(filename, file_in))
{
return 1;
}//opens file

start = disk.tell(file_in);
Size_of_file= disk.seek(position, file_in, disk);
bool endoffile = disk.eof(file_in);
if( !(Size_of_file 0) )
{
exit(0);
}

while(!endoffile || current_buff_size != 0)
{
output(disk.read(buffer, file_in, current_buff_size, file_index),
current_buff_size, endoffile);
Size_of_file--;
endoffile = disk.eof(file_in);
}

return 0;
}//end main

void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[16];
outBuff[curr_buffer_size] = ret_buffer;
cout<<setw(4)<<setfill('0')<<uppercase<<hex<<addr< <"0: "; //prints
mem location
for (int index = 0; index <16; index++)
{
index2++;
if (index2 <= curr_buffer_size)
cout<<hex<<setw(2)<<setfill('0')<<(int)outBuff[index];
else
cout<<" ";
cout<<" ";
}//end for
cout<<setfill(' ');
//cout<< " ");
index2 = 0;
for (int index = 0; index < 16; index ++)
{
index2++;
if (index2 <= curr_buffer_size)
{
if (outBuff[index] < 32 || outBuff[index] 126)
printf(".");
else
cout<<outBuff[index];
}//end if
}//end for
============================================

#ifndef DISK_H
#define DISK_H

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <fstream>
class Cdisk
{
public:

// DO NOT change or add to any of these public functions, except to
make
// minor syntax or type corrections.
// You will notice that these mimic the Linux binary file utilities,
// except that these only support file reading

// open a file for reading
// returns true if successful, false otherwise
bool open(const char* filename, std::ifstream& file_in);

// return true if read head is at the end of the file
// Can also tell from the bytes returned from 'read' whether at end
of file
bool eof(std::ifstream& file_in) const;

// read into the buffer, up to 'length' bytes, return number of
bytes
// actually read
unsigned char read(unsigned char* buffer, std::ifstream& file_in,
int& current_buff_size, int& file_index); // get_byte

// position the read/write head to 'position' relative
// to the file origin -- success returns true, failure returns
false.
// You will get a failure if position is negative or >= length of
the file.
// Positions are measured with 0 at the file origin.
int seek(int position, std::ifstream& file_in, Cdisk& disk);

// return the current position of the read/write head
// 0 marks the file origin.
int tell(std::ifstream& file_in) const;

// close the file
void close(std::ifstream& file_in);

};

Mar 1 '07 #1
4 1697
On Mar 1, 8:29 pm, "uche" <uraniumore...@hotmail.comwrote:
can someone please look at this code and decipher it for me....i've
been stressed by it!
// *STUFF*

maybe you can reorganize the code that someone bother looking at.

Mar 1 '07 #2
On 1 Mar 2007 04:29:32 -0800 in comp.lang.c++, "uche"
<ur***********@hotmail.comwrote,

Well, one reason you have problems is this:
>void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[16];
outBuff[curr_buffer_size] = ret_buffer;
curr_buffer_size is certainly much greater than 16, so you are assigning
to a position way past the end of the array and clobbering something
else.

The character returned from Cdisk::read() is useless. Do not try to
pass it directly to output(), that will not get you to the solution.
Instead you are going to need to pass a pointer to the buffer or the
part of the buffer where the data resides. Do not duplicate the buffer
inside output(), either.

My version of your main loop would look something more like this:

do {
current_buff_size = 0; // force brain-dead Cdisk to read
disk.read(buffer, file_in, current_buff_size, file_index);
current_buff_size += 1; // undo damage
for(int position = 0; position<current_buff_size; position+=16)
output(buffer+position, current_buff_size-position);
} while (current_buff_size 0) ;

Mar 1 '07 #3
David Harmon wrote:
On 1 Mar 2007 04:29:32 -0800 in comp.lang.c++, "uche"
<ur***********@hotmail.comwrote,

Well, one reason you have problems is this:

>>void output(unsigned char ret_buffer, int curr_buffer_size, bool&
endoffile)
{
int index2=0;
int addr=0;
unsigned char outBuff[16];
outBuff[curr_buffer_size] = ret_buffer;


curr_buffer_size is certainly much greater than 16, so you are assigning
to a position way past the end of the array and clobbering something
else.

The character returned from Cdisk::read() is useless. Do not try to
pass it directly to output(), that will not get you to the solution.
Instead you are going to need to pass a pointer to the buffer or the
part of the buffer where the data resides. Do not duplicate the buffer
inside output(), either.

My version of your main loop would look something more like this:

do {
current_buff_size = 0; // force brain-dead Cdisk to read
disk.read(buffer, file_in, current_buff_size, file_index);
current_buff_size += 1; // undo damage
for(int position = 0; position<current_buff_size; position+=16)
output(buffer+position, current_buff_size-position);
} while (current_buff_size 0) ;
As you say Cdisk is brain dead. But worse it looks like something that
the OP's tutor is telling him he must use.

OP, I'm not surprised you're struggling, learning C++ is difficult
enough without have having a tutor who can't write decent code.

john
Mar 1 '07 #4
On Thu, 01 Mar 2007 21:11:12 GMT in comp.lang.c++, John Harrison
<jo*************@hotmail.comwrote,
>As you say Cdisk is brain dead. But worse it looks like something that
the OP's tutor is telling him he must use.
Sure. I could not guess if it was "instructor supplied" or perhaps the
product of an earlier assignment. Either way the "can't fix it" dictum
is a substantial handicap -- you cannot hope to get a library interface
right until it has been used by someone, preferably several times.

Cdisk makes the assignment a fair bit harder than bare iostreams.
http://groups.google.com/gr*********....earthlink.net

Mar 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Justin Tang | last post by:
Hi I am wondering about the segmentation fault in PHP. Namely, I'm running PHP version 4.6.9 on my server right now and when I try to process a large piece of text via textarea(3k+), the resulting...
6
by: jerrygarciuh | last post by:
Hello, I have a script running in the wee hours via cron job. When I access the script via browser it works like a charm. I noticed it was not succeeding and ran it manually from the command...
11
by: Polar | last post by:
Hi! i'm a newbie in C language and i'm writing my first simple codes. In one of these, my purpose is to append the ascii value of an interger (example 101 --> e) at the end of a string to...
9
by: Narendran Kumaraguru Nathan | last post by:
Hi all, I am fairly experianced in C. I am writing a program in which I'm getting a segmentation fault. The problem is that it is getting the segmentation fault when executing calloc. I tried...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.