Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ I/O Rereading a file after EOF (was: Parsing a Comma Delimited Text File)
    primarykey
    data
    text
    <p>Lets take a look at the code:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; #include &lt;cstdio&gt; #include &lt;cstdlib&gt; 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 &lt;&lt; "Enter a file: "; cin &gt;&gt; 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 &lt; sLine.length(); x++ ) { if ( sLine[ x ] == ',' )break; // when we hit a comma stop reading //cout &lt;&lt; sLine[ x ]; } cout &lt;&lt; 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 &lt; sLine.length(); y++ ) { if ( sLine[ y ] == ',' )break; // hit a comma then goto seekp until eof cout &lt;&lt; sLine[ y ]; } cout &lt;&lt; endl; } inFile.clear(); inFile.close(); fgetc( stdin ); return 0; } </code></pre> <p>My text file format is similar to this:</p> <pre><code>string, string andthenspace, numbers, morenumbers </code></pre> <p>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.</p> <p>So the second time I thought, ok just the seekp(X, ios::cur) function to start there on each iteration of the second while..</p> <p>Unfortunately, it's not reading the file a second time..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload