seekg woos

This is a discussion on seekg woos within the C++ Programming forums, part of the General Programming Boards category; Trying to do some random file access. But seekg() or get() is not working as I expected... Of course what ...

  1. #1
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317

    Cool seekg woos

    Trying to do some random file access. But seekg() or get() is not working as I expected... Of course what I was expecting is the problem here
    Code:
       //.... declarations and stuff
       pifile.open("teste.txt", ios::in | ios::nocreate);
       if(!pfile) exit(1);
    
       cout << "Displaying the file:" << endl;
       while(pifile.get(inchar)) cout << inchar;
    
       cout << endl << "Returning to the begin:" << endl;
       pifile.seekg(0L, SEEK_SET);
       cout << "Reading first line only" << endl;
       for(int i=1; i<=7; i++)
       {
          if(pifile.get(inchar)) cout << inchar;
             else break;
       }
       //....
    The first while block displays the file nicely. But although I get no compile errors, after I seekg it to the beginning, the for loop that follows doesn't display anything. In fact while tracing, not even the break statement is processed. After testing if(pifile.get(inchar)), it immediatly goes to the end bracket.

    What am I doing wrong?
    Last edited by Mario; 05-26-2002 at 05:36 PM.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to clear the EOF flag after reading to the end of the file the first time.

    I think it's this:

    >pifile.clear();
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    It worked.

    Thanks a bunch Hammer
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    7

    Smile Help!!

    hi, i need, help, i want to now, how i can use the direct files, like fseek, etc, in a program , thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File copying using get() and seekg()
    By tonycorruption in forum C++ Programming
    Replies: 2
    Last Post: 01-15-2006, 12:05 PM
  2. Problem with fstream and seekg()
    By hdragon in forum C++ Programming
    Replies: 10
    Last Post: 09-24-2005, 01:50 PM
  3. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 10:10 PM
  4. Not sure if I should be using seekg()
    By manofsteel972 in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2004, 07:39 PM
  5. seekp(), seekg()
    By Dual-Catfish in forum C++ Programming
    Replies: 3
    Last Post: 03-29-2002, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21