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

U

uche

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::eek:pen(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);



};
 
P

Peyman

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.
 
D

David Harmon

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) ;
 
J

John Harrison

David said:
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
 
D

David Harmon

On Thu, 01 Mar 2007 21:11:12 GMT in comp.lang.c++, John Harrison
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/[email protected]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top