User Tools

Site Tools


cplusplus:file_processing_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
cplusplus:file_processing_examples [2017/02/08 20:46] – [Filenames must be c_str in pre-C++11] mithatcplusplus:file_processing_examples [2017/02/08 20:48] – [Reading multiple tokens] mithat
Line 144: Line 144:
 int main() int main()
 { {
-    const string filename = "some_numbers.txt";+    string filename = "some_numbers.txt";
     int num1, num2;     int num1, num2;
  
-    ifstream infile;   // instantiate file pointer object +    ifstream infile;         // instantiate file pointer object 
-    infile.open(filename.c_str());   // associate file with file pointer object+    infile.open(filename);   // associate file with file pointer object
  
     if (!infile)     if (!infile)
-        cerr << "Error opening file." << endl; 
-    else 
     {     {
-        cout << "Reading from file ... ";+        cout << "Error opening file." << endl; 
 +        return 1;  // exit program with error code 
 +    } 
 +     
 +    cout << "Reading from file ... ";
  
-        infile >> num1 >> num2; +    infile >> num1 >> num2; 
-        infile.close();    // close the file!+    infile.close();    // close the file!
  
-        cout << "Done!" << endl; +    cout << "Done!" << endl; 
-        cout << "The first two numbers in the file are:" << endl +    cout << "The first two numbers in the file are:" << endl 
-             << num1 << endl +         << num1 << endl 
-             << num2 << endl +         << num2 << endl 
-             << endl; +         << endl;
-    }+
  
     return 0;     return 0;
cplusplus/file_processing_examples.txt · Last modified: 2019/02/11 19:17 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki