Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > endless loop

Reply
Thread Tools

endless loop

 
 
uche
Guest
Posts: n/a
 
      02-28-2007
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
}
}

 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      02-28-2007
On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
<(E-Mail Removed)> wrote,
>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?

 
Reply With Quote
 
 
 
 
uche
Guest
Posts: n/a
 
      02-28-2007
On Feb 27, 8:00 pm, David Harmon <(E-Mail Removed)> wrote:
> On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
> <(E-Mail Removed)> wrote,
>
> >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?


=================
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
}

}

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      02-28-2007
On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
<(E-Mail Removed)> wrote,
>On Feb 27, 8:00 pm, David Harmon <(E-Mail Removed)> wrote:
>> On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
>> <(E-Mail Removed)> wrote,
>>
>> >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?

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

 
Reply With Quote
 
uche
Guest
Posts: n/a
 
      02-28-2007
On Feb 27, 8:51 pm, David Harmon <(E-Mail Removed)> wrote:
> On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
> <(E-Mail Removed)> wrote,
>
> >On Feb 27, 8:00 pm, David Harmon <(E-Mail Removed)> wrote:
> >> On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
> >> <(E-Mail Removed)> wrote,

>
> >> >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?

>


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: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
> >=================
> >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.



 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      02-28-2007
On 27 Feb 2007 21:20:06 -0800 in comp.lang.c++, "uche"
<(E-Mail Removed)> wrote,
>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.

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      02-28-2007
uche wrote:
> On Feb 27, 8:51 pm, David Harmon <(E-Mail Removed)> wrote:
>
>>On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
>><(E-Mail Removed)> wrote,
>>
>>
>>>On Feb 27, 8:00 pm, David Harmon <(E-Mail Removed)> wrote:
>>>
>>>>On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
>>>><(E-Mail Removed)> wrote,

>>
>>>>>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?

>>

>
> 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: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
>
>>>=================
>>>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.

>
>
>


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Can a simple a==b 'hang' in and endless loop? Claudio Grondi Python 75 01-21-2006 04:19 AM
endless loop and i dont understand why Alexandre Java 4 04-12-2005 07:20 PM
Endless loop on delete auth cookie/abandon session =?Utf-8?B?QmlsbCBCb3Jn?= ASP .Net 0 02-02-2005 08:05 PM
exit endless python loop in emacs Leo Python 1 07-04-2003 12:58 PM



Advertisments