Note that there are some explanatory texts on larger screens.

plurals
  1. POFile I/O logic with a while statement, how would this code be expected to behave?
    primarykey
    data
    text
    <p>I'm trying to understand some differences in file i/o techniques. Suppose I have the following code:</p> <pre><code>FILE *work_fp; char record[500] = {0}; while(!feof(work_fp)) { static int first = 1; fgets(record, 200, work_fp); if (first) { var1 = 2; length += var1; } first = 0; if (feof(work_fp)) { continue; } if((int)strlen(record) &lt; length) { fclose(work_fp); std::ostringstream err; err &lt;&lt; "ERROR -&gt; Found a record with fewer bytes than required in file." &lt;&lt; std::endl; throw std::runtime_error(err.str()); } const int var2 = 1; if(memcmp(argv[1], record + var2, 3) == 0) { load_count_struct(record, var1); } } </code></pre> <p>I'm not seeing how the second <code>if</code> argument can be true.</p> <pre><code>if (feof(work_fp)) { continue; } </code></pre> <p>If <code>feof(work_fp)</code> is true wouldn't the <code>while</code> argument be false? Then the continue could never get called?</p> <p>FOLLOW UP QUESTION:</p> <p>Ok, I see how <code>fgets</code> can cause <code>work_fp</code> to reach <code>eof</code> conditions.</p> <p>Suppose I want to try and implement this another way. Using <code>getline()</code>, for example.</p> <pre><code>std::string data(file); std::ifstream in(data.c_str()); if (!in.is_open()) { std::ostringstream err; err &lt;&lt; "Cannot open file: " &lt;&lt; file &lt;&lt; std::endl; throw std::runtime_error(err.str()); } std::string buffer = ""; std::string record = ""; while (getline(in, buffer)) { static int first = 1; if (first) { var1 = 2; length += var1; } first = 0; if (//What should go here?!?) { break; } // etc... } </code></pre> <p>Any suggestions? I'm thinking</p> <pre><code>if (buffer == std::string::npos) </code></pre> <p>no?</p>
    singulars
    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