tags:

views:

1147

answers:

3

Lets take a look at the code:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>


using namespace std;


int main()
{
    string usrFileStr,
    fileStr = "airNames.txt",  // declaring string literal
    sLine;                        // declaring a string obj

    fstream inFile;                  // declaring a fstream obj
    char ch;

    cout << "Enter a file: ";
    cin >> usrFileStr;


    inFile.open( usrFileStr.c_str(), ios::in ); 
    // at this point the file is open and we may parse the contents of it


    while ( !inFile.eof() )
    {
          getline ( inFile, sLine ); // store contents of txt file into str Obj
          for ( int x = 0; x < sLine.length(); x++ )
          {         
              if ( sLine[ x ] == ',' )break; // when we hit a comma stop reading 
              //cout << sLine[ x ];
          }
          cout << endl;
    }      



        while ( !inFile.eof() )  //read the file again until we reach end of file
        {
                // we will always want to start at this current postion;
              inFile.seekp( 6L, ios::cur );

              getline( inFile, sLine ); // overwrite the contents of sLine
              for ( int y = 0; y < sLine.length(); y++ )
              {
                  if ( sLine[ y ] == ',' )break; // hit a comma then goto seekp until eof
                  cout << sLine[ y ];
              }
              cout << endl;
        }

    inFile.clear();
    inFile.close();



    fgetc( stdin );
    return 0;
}

My text file format is similar to this:

string, string andthenspace, numbers, morenumbers

It doesn't look like I'm able to read the file twice..Checking for EOF each time.. The first while condition works, and it gives me what I need, the first field before the comma, and not including the comma.

So the second time I thought, ok just the seekp(X, ios::cur) function to start there on each iteration of the second while..

Unfortunately, it's not reading the file a second time..

+3  A: 

You probably need to clear the error bit on the stream after reading EOF the first time. That is, as well as doing the seek operation.

Jonathan Leffler
+1  A: 

You seem to be going the wrong way about it:

FILE * file;
file = fopen ("file.txt","r");


if (file!=NULL){
        while (!feof(file)) {           
                fscanf(file,"%s ,%s ,%s ,%s",str1,str2,str3,str4);
        }
        fclose (file);
};

You can obviously expand the above example, I haven't had time to check it up it should work.

Jamie Lewis
That's a rather C-like solution to a C++ problem, isn't it? Why do you consider using the native C++ i/o operators incorrect?
Jonathan Leffler
This doesn't even say how to go back to the beginning of the file.
Jay Conrod
You didn't address his problem of rewinding the file at all.
Rob K
+2  A: 

First, never test eof in your while loops, the flag will be true after you hit the end of the file, i.e. too late, the test must be done just after the read, and before using what you expect to have read. ->

while (std::getline(......))

Then, Jonathan Leffler gave you the solution, clear the state bits before the call to seekg. However, your approach seems somewhat complex as Jamie pointed out.

Luc Hermitte
+1 but the situation is really a bit more complex than that, as EOF being set after a read doesn't mean that the read failed, either.
Tronic