cplusplus:file_processing_examples
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
cplusplus:file_processing_examples [2013/03/01 01:07] – created mithat | cplusplus:file_processing_examples [2017/02/08 20:45] – [Checking for opening errors] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== File processing examples ====== |
===== File writing ===== | ===== File writing ===== | ||
+ | |||
+ | ==== Simple example ==== | ||
+ | |||
+ | <file c++ simple-file-write.cpp>// | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | ofstream outfile; | ||
+ | outfile.open(" | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile.close(); | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Checking for opening errors ==== | ||
+ | |||
+ | <file c++ simple-file-write2.cpp>// | ||
+ | // Shows error checking. | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | ofstream outfile; | ||
+ | outfile.open(" | ||
+ | |||
+ | if (!outfile) | ||
+ | { | ||
+ | cout << "Error opening file." << endl; | ||
+ | return 1; // exit program with an error code | ||
+ | } | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile.close(); | ||
+ | |||
+ | cout << " | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Filenames must be c_str in pre-C++11 ==== | ||
+ | |||
+ | <file c++ simple-file-write3.cpp>// | ||
+ | // Converting string to c_str. | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | ofstream outfile; | ||
+ | string filename; | ||
+ | |||
+ | // get filename from user | ||
+ | cout << "Enter the filename to store the file in\n" | ||
+ | << | ||
+ | cin >> filename; | ||
+ | |||
+ | outfile.open(filename.cstr()); | ||
+ | |||
+ | if (!outfile) | ||
+ | cout << "Error opening file." << endl; | ||
+ | else | ||
+ | { | ||
+ | cout << " | ||
+ | |||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile << " | ||
+ | outfile.close(); | ||
+ | |||
+ | cout << " | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
===== File reading ===== | ===== File reading ===== | ||
+ | ==== Simple example ==== | ||
+ | <file c++ simple-file-read.cpp>// | ||
+ | #include < | ||
+ | #include < | ||
+ | #include < | ||
+ | using namespace std; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const string filename = " | ||
+ | int num1; | ||
+ | |||
+ | ifstream infile; | ||
+ | infile.open(filename.c_str()); | ||
+ | // (file name needs to be a c_str) | ||
+ | |||
+ | if (!infile) | ||
+ | cerr << "Error opening file." << endl; | ||
+ | else | ||
+ | { | ||
+ | cout << " | ||
+ | |||
+ | infile >> num1; | ||
+ | infile.close(); | ||
+ | |||
+ | cout << " | ||
+ | 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() | ||
+ | { | ||
+ | const string filename = " | ||
+ | int num1, num2; | ||
+ | |||
+ | ifstream infile; | ||
+ | infile.open(filename.c_str()); | ||
+ | |||
+ | if (!infile) | ||
+ | cerr << "Error opening file." << endl; | ||
+ | else | ||
+ | { | ||
+ | cout << " | ||
+ | |||
+ | infile >> num1 >> num2; | ||
+ | infile.close(); | ||
+ | |||
+ | cout << " | ||
+ | cout << "The first two numbers in the file are:" << endl | ||
+ | << | ||
+ | << | ||
+ | << | ||
+ | } | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== 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