endless loop

U

uche

This function that I have implemented gives me an infinite loop. I am
trying to produce a hexdum program, however, this function is not
functioning correctly.....Please help.

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

while (!endoffile)
{
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
}
}
 
D

David Harmon

On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
This function that I have implemented gives me an infinite loop. I am ....
while (!endoffile)

endoffile is never changed within the function posted, so of course you
have either an infinite loop or no loop. What was supposed to change
the value of endoffile?
 
U

uche

On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"


endoffile is never changed within the function posted, so of course you
have either an infinite loop or no loop. What was supposed to change
the value of endoffile?

=================
Here is the 'main' code:

#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);
}

do
{

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


}//end main
unsigned char retrieve_bytes(unsigned char* buffer, ifstream& file_in,
int& current_buff_size, int& file_index, Cdisk& disk )
{
unsigned char get = disk.read(buffer, file_in, current_buff_size,
file_index);
return ( get );

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

while (!endoffile)
{
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
}

}
 
D

David Harmon

On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
=================
Here is the 'main' code:

But without _your_ answer to the above question "What was supposed to
change the value of endoffile?", the loop is still endless.
 
U

uche

On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"

the disk.eof(file_in); was supposed to change the value of the
endoffile variable..Here is the code for the eof class function...

#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_in.read((char*)buffer, 512);
current_buff_size =file_in.gcount();
}
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
 
D

David Harmon

On 27 Feb 2007 21:20:06 -0800 in comp.lang.c++, "uche"
the disk.eof(file_in); was supposed to change the value of the
endoffile variable.

That line cannot affect the endless loop because it is outside the loop,
and in fact outside the entire output() function. That line doesn't get
a chance to execute while the output() function is in control, but only
after the endless loop terminates and output() returns, which of course
doesn't happen.

It is time for you to go through your code line-by-line as if you were
the computer executing it, and make sure that every line really says
what YOU INTENDED for it to say, and that it makes sense at that point
in the program. There are still quite a few howlers lurking there.
 
J

John Harrison

uche said:
the disk.eof(file_in); was supposed to change the value of the
endoffile variable..Here is the code for the eof class function...

#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_in.read((char*)buffer, 512);
current_buff_size =file_in.gcount();
}
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
But without _your_ answer to the above question "What was supposed to
change the value of endoffile?", the loop is still endless.

As David and Jack have said there is lots and lots wrong with your code.
But to answer your specific question if you look at your code (and try
to view it as a compiler would, like David said). You will see that you
have two loops, in main you have

do
{
...
}
while (!endoffile || current_buff_size != 0);

and in output you have

while (!endoffile)
{
...
}

I'm sure you can see that as you only have one file, you only need one
loop to go through the contents of that file. So fundamentally your
program is not working bcause of specific mistakes (though you have a
few of those) but because the *design* is wrong.

Rewrite you code so that it has only one loop, and you'll be closer to
getting a working program. Personally I'd throw away what you have now
and start again. It will be easier second time around. And remember to
go slowly, don't try to write the program all at once.

john
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top