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