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:25] – mithat | cplusplus:file_processing_examples [2017/02/08 20:52] – [Reading multiple tokens] mithat | ||
---|---|---|---|
Line 42: | Line 42: | ||
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 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 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 104: | Line 106: | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include <cstring> | + | #include <string> |
using namespace std; | using namespace std; | ||
int main() | int main() | ||
{ | { | ||
- | | + | string filename = " |
int num1; | int num1; | ||
- | ifstream infile; | + | ifstream infile; |
- | infile.open(filename.c_str()); // associate file with file pointer object | + | infile.open(filename); |
- | // (file name needs to be a c_str) | + | |
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 number in the file is " << num1 << " | + | cout << "The first number in the file is " << num1 << " |
- | } | + | |
return 0; | return 0; | ||
Line 137: | Line 139: | ||
#include < | #include < | ||
#include < | #include < | ||
- | #include <cstring> | + | #include <string> |
using namespace std; | using namespace std; | ||
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