endless loop

Discussion in 'C++' started by uche, Feb 28, 2007.

  1. uche

    uche Guest

    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
    }
    }
    uche, Feb 28, 2007
    #1
    1. Advertising

  2. uche

    David Harmon Guest

    On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
    <> 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?
    David Harmon, Feb 28, 2007
    #2
    1. Advertising

  3. uche

    uche Guest

    On Feb 27, 8:00 pm, David Harmon <> wrote:
    > On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
    > <> 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
    }

    }
    uche, Feb 28, 2007
    #3
  4. uche

    David Harmon Guest

    On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
    <> wrote,
    >On Feb 27, 8:00 pm, David Harmon <> wrote:
    >> On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
    >> <> 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.
    David Harmon, Feb 28, 2007
    #4
  5. uche

    uche Guest

    On Feb 27, 8:51 pm, David Harmon <> wrote:
    > On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
    > <> wrote,
    >
    > >On Feb 27, 8:00 pm, David Harmon <> wrote:
    > >> On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
    > >> <> 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::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
    > >=================
    > >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.
    uche, Feb 28, 2007
    #5
  6. uche

    David Harmon Guest

    On 27 Feb 2007 21:20:06 -0800 in comp.lang.c++, "uche"
    <> 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.
    David Harmon, Feb 28, 2007
    #6
  7. uche wrote:
    > On Feb 27, 8:51 pm, David Harmon <> wrote:
    >
    >>On 27 Feb 2007 20:25:44 -0800 in comp.lang.c++, "uche"
    >><> wrote,
    >>
    >>
    >>>On Feb 27, 8:00 pm, David Harmon <> wrote:
    >>>
    >>>>On 27 Feb 2007 19:53:33 -0800 in comp.lang.c++, "uche"
    >>>><> 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::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
    >
    >>>=================
    >>>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
    John Harrison, Feb 28, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbCBCb3Jn?=

    Endless loop on delete auth cookie/abandon session

    =?Utf-8?B?QmlsbCBCb3Jn?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    661
    =?Utf-8?B?QmlsbCBCb3Jn?=
    Feb 2, 2005
  2. Alexandre
    Replies:
    4
    Views:
    468
    Lasse Reichstein Nielsen
    Apr 12, 2005
  3. Leo
    Replies:
    1
    Views:
    404
    Thomas Heller
    Jul 4, 2003
  4. Claudio Grondi
    Replies:
    75
    Views:
    1,112
    Tim Peters
    Jan 21, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page