Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Besides the obvious check for <code>argc != 2</code> I couldn't help fixing some of the worse code and the obvious error:</p> <pre><code>#include &lt;iostream&gt; #include &lt;fstream&gt; #include &lt;string&gt; using namespace std; int main (int argc, char* argv[]) { if (argc != 2) { cout &lt;&lt; "ERROR. Invalid number of arguments\n"; return 1; } ifstream infile(argv[1]); if (!infile) // covers a miss spelling of a fail name { cout &lt;&lt; "ERROR. Did you make a mistake in the Spelling of the File\n"; return 1; } string STRING; while(getline(infile, STRING)) cout &lt;&lt; STRING &lt;&lt; '\n'; // Prints out file line return 0; } </code></pre> <p>You don't need to call <code>ifstream::open</code>, just use the constructor, likewise don't you need to declare <code>STRING</code> so early and neither to initialize it to the file name, since you don't use it. Don't forget, this is not C, you don't need a whole mess of declarations at the beginning of each function.</p> <p>Second, checking for the flags of a stream is often a bad idea, just check for <code>!infile</code> to find any errors. But the real error is in checking for <code>infile.eof</code> in the while condition, since it only gets set once <code>getline</code> has tryed to read over the end of the file, so you would actually print one (probably empty) line too much. Just check for <code>getline</code>'s return value to find any errors or the end of file.</p> <p>And don't add the newline onto the string when outputting, just put it out after the string. And last but not least, no need for <code>infile.close</code>, since the destructor calls it anyway.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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