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
cplusplus:file_processing_examples [2013/03/01 01:19] mithatcplusplus:file_processing_examples [2019/02/11 19:17] (current) – [Using filenames] 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 59: Line 60:
 </file> </file>
  
-==== Filenames must be c_str ====+==== Using filenames ====
  
 <file c++ simple-file-write3.cpp>// This program writes data to a file. <file c++ simple-file-write3.cpp>// This program writes data to a file.
Line 78: Line 79:
     cin >> filename;     cin >> filename;
  
-    outfile.open(filename);   // associate file with file pointer object+    outfile.open(filename);            // associate file with file pointer object 
 +    //outfile.open(filename.c_str());  // convert filename to C-string in pre-C++11
  
     if (!outfile)     if (!outfile)
 +    {
         cout << "Error opening file." << endl;         cout << "Error opening file." << endl;
-    else+        return 1;  // exit program with an error code 
 +    } 
 + 
 +    cout << "Writing to the file ... "; 
 + 
 +    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; 
 +
 +</file> 
 + 
 +===== File reading ===== 
 + 
 +==== Simple example ==== 
 +<file c++ simple-file-read.cpp>// This program reads a number from a file. 
 +#include <iostream> 
 +#include <fstream> 
 +#include <string> 
 +using namespace std; 
 + 
 +int main() 
 +
 +    string filename = "some_numbers.txt"; 
 +    int num1; 
 + 
 +    ifstream infile;         // instantiate file pointer object 
 +    infile.open(filename);   // associate file with file pointer object 
 + 
 +    if (!infile)
     {     {
-        cout << "Writing to the file ... ";+        cout << "Error opening file." << endl; 
 +        return 1;  // exit program with error code 
 +    } 
 +     
 +    cout << "Reading from file ... ";
  
-        outfile << "KCMP\t" << 89.3 << endl+    infile >> num1
-        outfile << "KNOW\t" << 91.1 << endl; +    infile.close();    // close the file!
-        outfile << "KSJN\t" << 99.5 << endl; +
-        outfile.close();    // close the file!+
  
-        cout << "Done!" << endl;+    cout << "Done!" << endl; 
 +    cout << "The first number in the file is " << num1 << "." << endl; 
 + 
 +    return 0; 
 +
 +</file> 
 + 
 +==== Reading multiple tokens ==== 
 +<file c++ simple-file-read2.cpp>// This program reads two numbers from a file. 
 +#include <iostream> 
 +#include <fstream> 
 +#include <string> 
 +using namespace std; 
 + 
 +int main() 
 +
 +    string filename = "some_numbers.txt"; 
 +    int num1, num2; 
 + 
 +    ifstream infile;         // instantiate file pointer object 
 +    infile.open(filename);   // associate file with file pointer object 
 + 
 +    if (!infile) 
 +    { 
 +        cout << "Error opening file." << endl; 
 +        return 1;  // exit program with error code
     }     }
 +    
 +    cout << "Reading from file ... ";
 +
 +    infile >> num1 >> num2;
 +    infile.close();    // close the file!
 +
 +    cout << "Done!" << endl;
 +    cout << "The first two numbers in the file are:" << endl
 +         << num1 << endl
 +         << num2 << endl
 +         << endl;
  
     return 0;     return 0;
Line 98: Line 172:
 </file> </file>
  
-===== File reading =====+==== Read all tokens in a file ==== 
 +<file c++ simple-file-read3.cpp>// This program reads all the numbers from a file. 
 +#include <iostream> 
 +#include <fstream> 
 +#include <string> 
 +using namespace std;
  
 +int main()
 +{
 +    const string filename = "some_numbers.txt";
 +    int num1;
  
 +    ifstream infile;         // instantiate file pointer object
 +    infile.open(filename);   // associate file with file pointer object
  
 +    if (!infile)
 +    {
 +        cout << "Error opening file." << endl;
 +        return 1;  // exit program with error code
 +    }
 +
 +    cout << "Reading from file ... " << endl;
 +
 +    while (infile >> num1)
 +    {
 +        cout << num1 << endl;
 +    }
 +    infile.close();    // close the file!
 +
 +    cout << "Done!" << endl;
 +
 +    return 0;
 +}
 +</file>
cplusplus/file_processing_examples.1362100771.txt.gz · Last modified: 2013/03/01 01:19 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki