first, lets have a look at the format of my test.txt file:
ABCDE,somestrings andaspace,somenumbers,morenumbers
After i open the file, i have a while loop that has a nested a for loop and an if to test for the comma. i then break out of the loop to stop reading characters. My output is the first feild, for every line of the file.
Now, i'll do the same thing using a fresh loop with the same condition.Execpt i want to start at the fifth character for every line, until, of course, the loop reaches end of file.
To better explain lets look at some code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string usrFileStr,
fileStr = "test.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
{
inFile.seekp( 6L, ios::cur ); // we will always want to start at this postion when reading the second feild
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;
}
|
I cant even get a simple loop to work after the first...
while ( getline( infile, sBuffer ) ) { cout << sBuffer << endl; }
doesnt give me the contents of the file. When it works fine the first time..
why is this?
And I'd really like to keep going, but i cant output anything for the second while loop.
I really dont want to go in a new direction here, is there something i can do to make it work without choosing a new path?