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 revision
Previous revision
Next revisionBoth sides next revision
cplusplus:file_processing_examples [2016/09/27 02:13] – [Filenames must be c_str in C++98 and earlier] mithatcplusplus:file_processing_examples [2017/02/08 20:54] – [Read all tokens in a file] mithat
Line 42: Line 42:
  
     if (!outfile)     if (!outfile)
-        cout << "Error opening file." << endl; 
-    else 
     {     {
-        cout << "Writing to the file ... ";+        cout << "Error opening file." << endl; 
 +        return 1 // exit program with an error code 
 +    }
  
-        outfile << "KCMP\t" << 89.3 << endl; +    cout << "Writing to the file ... ";
-        outfile << "KNOW\t" << 91.1 << endl; +
-        outfile << "KSJN\t" << 99.5 << endl; +
-        outfile.close();    // close the file!+
  
-        cout << "Done!" << endl; +    outfile << "KCMP\t" << 89.3 << endl; 
-    }+    outfile << "KNOW\t" << 91.1 << endl; 
 +    outfile << "KSJN\t" << 99.5 << endl; 
 +    outfile.close();    // close the file! 
 + 
 +    cout << "Done!" << endl;
  
     return 0;     return 0;
Line 81: Line 82:
  
     if (!outfile)     if (!outfile)
-        cout << "Error opening file." << endl; 
-    else 
     {     {
-        cout << "Writing to the file ... ";+        cout << "Error opening file." << endl; 
 +        return 1 // exit program with an error code 
 +    }
  
-        outfile << "KCMP\t" << 89.3 << endl; +    cout << "Writing to the file ... ";
-        outfile << "KNOW\t" << 91.1 << endl; +
-        outfile << "KSJN\t" << 99.5 << endl; +
-        outfile.close();    // close the file!+
  
-        cout << "Done!" << endl; +    outfile << "KCMP\t" << 89.3 << endl; 
-    }+    outfile << "KNOW\t" << 91.1 << endl; 
 +    outfile << "KSJN\t" << 99.5 << endl; 
 +    outfile.close();    // close the file! 
 + 
 +    cout << "Done!" << endl;
  
     return 0;     return 0;
Line 104: Line 106:
 #include <iostream> #include <iostream>
 #include <fstream> #include <fstream>
-#include <cstring>+#include <string>
 using namespace std; using namespace std;
  
 int main() int main()
 { {
-    const string filename = "some_numbers.txt";+    string filename = "some_numbers.txt";
     int num1;     int num1;
  
-    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
-                                     // (file name needs to be a c_str)+
  
     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; +    infile >> num1; 
-        infile.close();    // close the file!+    infile.close();    // close the file!
  
-        cout << "Done!" << endl; +    cout << "Done!" << endl; 
-        cout << "The first number in the file is " << num1 << "." << endl; +    cout << "The first number in the file is " << num1 << "." << endl;
-    }+
  
     return 0;     return 0;
Line 137: Line 139:
 #include <iostream> #include <iostream>
 #include <fstream> #include <fstream>
-#include <cstring>+#include <string>
 using namespace std; using namespace std;
  
 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;
Line 172: Line 175:
 #include <iostream> #include <iostream>
 #include <fstream> #include <fstream>
-#include <cstring>+#include <string>
 using namespace std; using namespace std;
  
Line 180: Line 183:
     int num1;     int num1;
  
-    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 ... " << endl;+        cout << "Error opening file." << endl; 
 +        return 1;  // exit program with error code 
 +    }
  
-        while (infile >> num1) +    cout << "Reading from file ... " << endl;
-        { +
-            cout << num1 << endl; +
-        } +
-        infile.close();    // close the file!+
  
-        cout << "Done!" << endl;+    while (infile >> num1) 
 +    { 
 +        cout << num1 << endl;
     }     }
 +    infile.close();    // close the file!
 +
 +    cout << "Done!" << endl;
  
     return 0;     return 0;
 } }
 </file> </file>
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