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 [2016/09/27 02:13] – [Filenames must be c_str in C++98 and earlier] mithat | cplusplus:file_processing_examples [2017/02/08 20:48] – [Reading multiple tokens] 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 81: | Line 82: | ||
if (!outfile) | if (!outfile) | ||
- | cout << "Error opening file." << endl; | ||
- | else | ||
{ | { | ||
- | cout << "Writing to the file ... "; | + | cout << "Error opening |
+ | return 1; // exit program with an error code | ||
+ | } | ||
- | outfile | + | cout << "Writing to the file ... "; |
- | outfile << " | + | |
- | outfile << | + | |
- | outfile.close(); | + | |
- | | + | outfile << " |
- | } | + | outfile << " |
+ | outfile << " | ||
+ | outfile.close(); | ||
+ | |||
+ | | ||
return 0; | return 0; | ||
Line 142: | Line 144: | ||
int main() | int main() | ||
{ | { | ||
- | | + | string filename = " |
int num1, num2; | int num1, num2; | ||
- | ifstream infile; | + | ifstream infile; |
- | infile.open(filename.c_str()); // associate file with file pointer object | + | infile.open(filename); |
if (!infile) | if (!infile) | ||
- | cerr << "Error opening file." << endl; | ||
- | else | ||
{ | { | ||
- | cout << " | + | |
+ | return 1; // exit program with error code | ||
+ | } | ||
+ | |||
+ | | ||
- | | + | |
- | infile.close(); | + | infile.close(); |
- | | + | |
- | cout << "The first two numbers in the file are:" << endl | + | cout << "The first two numbers in the file are:" << endl |
- | | + | |
- | | + | |
- | | + | |
- | } | + | |
return 0; | return 0; |
cplusplus/file_processing_examples.txt · Last modified: 2019/02/11 19:17 by mithat