cplusplus:file_processing_examples
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:file_processing_examples [2013/03/01 01:19] – mithat | cplusplus:file_processing_examples [2017/02/08 20:51] – [Simple example] mithat | ||
---|---|---|---|
Line 42: | Line 42: | ||
if (!outfile) | if (!outfile) | ||
+ | { | ||
cout << "Error opening file." << endl; | cout << "Error opening file." << endl; | ||
- | else | + | return 1; // exit program with an error code |
- | | + | |
- | cout << " | + | |
- | outfile | + | cout << "Writing to the file ... "; |
- | outfile << " | + | |
- | outfile << | + | |
- | outfile.close(); | + | |
- | | + | outfile << " |
- | } | + | outfile << " |
+ | outfile << " | ||
+ | outfile.close(); | ||
+ | |||
+ | | ||
return 0; | return 0; | ||
Line 59: | Line 60: | ||
</ | </ | ||
- | ==== Filenames must be c_str ==== | + | ==== Filenames must be c_str in pre-C++11 |
<file c++ simple-file-write3.cpp>// | <file c++ simple-file-write3.cpp>// | ||
Line 78: | Line 79: | ||
cin >> filename; | cin >> filename; | ||
- | outfile.open(filename); | + | outfile.open(filename.cstr()); // associate file with file pointer object |
if (!outfile) | if (!outfile) | ||
+ | { | ||
cout << "Error opening file." << endl; | cout << "Error opening file." << endl; | ||
- | else | + | return 1; // exit program with an error code |
+ | } | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile.close(); | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== File reading ===== | ||
+ | |||
+ | ==== Simple example ==== | ||
+ | <file c++ simple-file-read.cpp>// | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | string filename = " | ||
+ | int num1; | ||
+ | |||
+ | ifstream infile; | ||
+ | infile.open(filename); | ||
+ | |||
+ | if (!infile) | ||
{ | { | ||
- | cout << "Writing to the file ... "; | + | cout << "Error opening file." << endl; |
+ | return 1; // exit program with error code | ||
+ | } | ||
+ | |||
+ | cout << " | ||
- | outfile << " | + | infile >> num1; |
- | | + | |
- | outfile << " | + | |
- | outfile.close(); | + | |
- | | + | |
+ | cout << "The first number in the file is " << num1 << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Reading multiple tokens ==== | ||
+ | <file c++ simple-file-read2.cpp>// | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | string filename = " | ||
+ | int num1, num2; | ||
+ | |||
+ | ifstream infile; | ||
+ | infile.open(filename); | ||
+ | |||
+ | if (!infile) | ||
+ | { | ||
+ | cout << "Error opening file." << endl; | ||
+ | return 1; // exit program with error code | ||
} | } | ||
+ | | ||
+ | cout << " | ||
+ | |||
+ | infile >> num1 >> num2; | ||
+ | infile.close(); | ||
+ | |||
+ | cout << " | ||
+ | cout << "The first two numbers in the file are:" << endl | ||
+ | << | ||
+ | << | ||
+ | << | ||
return 0; | return 0; | ||
Line 98: | Line 171: | ||
</ | </ | ||
- | ===== File reading ===== | + | ==== Read all tokens in a file ==== |
+ | <file c++ simple-file-read3.cpp>// | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | int main() | ||
+ | { | ||
+ | const string filename = " | ||
+ | int num1; | ||
+ | ifstream infile; | ||
+ | infile.open(filename.c_str()); | ||
+ | if (!infile) | ||
+ | cerr << "Error opening file." << endl; | ||
+ | else | ||
+ | { | ||
+ | cout << " | ||
+ | |||
+ | while (infile >> num1) | ||
+ | { | ||
+ | cout << num1 << endl; | ||
+ | } | ||
+ | infile.close(); | ||
+ | |||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ |
cplusplus/file_processing_examples.txt · Last modified: 2019/02/11 19:17 by mithat