Programming File I/O
Files in a personal computer environment Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. Files in a personal computer environment
Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide input data to a program or receive output data from a program, or both; must reside in Project directory (not necessarily the same directory as the .cpp files) must be opened before it is used.
Using Input/Output Files stream - a sequence of characters interactive (iostream) cin - input stream associated with keyboard. cout - output stream associated with display. file (fstream) ifstream - defines new input stream (normally associated with a file). ofstream - defines new output stream (normally associated with a file).
C++ streams Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.
Input File-Related Functions #include <fstream> ifstream fsIn; fsIn.open("fname.txt") connects stream fsIn to the external file "fname.txt ". fsIn.close() disconnects the stream and associated file. fsIn >> c; //Behaves just like cin
Output File-Related Functions #include <fstream> ofstream fsOut; fsOut.open("fname.txt") connects stream fsOut to the external file "fname.txt". fsOut.close() disconnects the stream and associated file. fsOut << c; //Behaves just like cout
File I/O: Example 1 // Reads three numbers from the file numbers.dat, // sums the numbers, and writes the sum to the file // sum.dat. #include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open("numbers.dat"); // open the output file output_stream.open("sum.dat");
File I/O: Example 1 // declare variables and read in the data (3 numbers) int first, second, third; input_stream >> first >> second >> third; // write the data to the output file output_stream << "The sum of the first 3\n" << "numbers in input_file.dat\n" << "is " << (first + second + third) << endl; // close the input and output files input_stream.close(); output_stream.close(); return 0; }
File I/O: Example 1.2 // copy everything from input.txt to output.txt #include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open(“input.txt"); // open the output file output_stream.open(“output.txt");
File I/O: Example 1.2 input_stream.get(next); char next; input_stream.get(next); while (! input_stream.eof()) { cout << next; output_stream.put(next); } input_stream.close(); output_stream.close(); return 0;
File I/O: Example 1.3 // Reads all the numbers from the file numbers.dat, // sums the numbers, and writes the sum and average to // the file sum.dat. #include <iostream> // for cin, cout #include <fstream> // for ifstream, ostream using namespace std; int main(){ // declare the input and output streams ifstream input_stream; ofstream output_stream; // open the input file input_stream.open("numbers.txt"); // open the output file output_stream.open("sum.dat");
// declare variables and read in the data int first, sum =0, counter= 0; input_stream >> first; while (!input_stream.eof()){ //test if is endoffile sum +=first; counter++; } cout << "The sum of numbers in the file is "<< sum << endl; cout << "The average is " << double(sum)/counter << endl; // write the data to the output file output_stream << "The sum is " << sum << endl; output_stream << "The average is “ << double(sum)/counter << endl; // close the input and output files input_stream.close(); output_stream.close(); return 0;
Example 2 //copies indata.dat to outdata.dat and counts the number of lines. Prints //file to screen too. #include <iostream> #include <fstream> using namespace std; int main(){ ifstream ins; ofstream outs; int count=0; char next; ins.open("indata.dat"); // open the input file outs.open("outdata.dat"); // open the output file
Example 2 while (ins.get(next)) { if (next=='\n') { count++; } cout << next; outs << next; if (next !='\n') {// when last line not end with ‘/n’ cout << endl; outs << endl; ins.close(); outs.close(); cout << "Number of lines copied: " << count << endl;
Example 3 Write a program which counts the number of blanks on each line of indata.dat, outputs each line, and number of blanks on each line. indata.dat: a b c top10 methods to count spaces 1 3(eof) Output to screen: Blanks: 2 Blanks: 4 Blanks: 0 1 3 Blanks: 3
File I/O: Example 3 // Counts the number of blanks on each line of the file // indata1.txt #include <iostream> #include <fstream> using namespace std; int main(){ ifstream ins; int count; char next; ins.open("indata1.txt"); // open the file ins.get(next); // get the first char
File I/O: Example 3 while(!ins.eof()){ // loop to read each line count = 0; while(next!='\n' ){ cout << next; if(next==' ') count++; ins.get(next); } cout << endl << "Blanks: " << count << endl; ins.close(); return 0;
Summary of Input File-Related Functions #include <fstream> ifstream fsIn; fsIn.open(const char[] fname) connects stream fsIn to the external file fname. fsIn.get(char& c) extracts next character from the input stream fsIn and places it in the character variable c. fsIn.eof() tests for the end-of-file condition. fsIn.close() disconnects the stream and associated file. fsIn >> c; //Behaves just like cin
Summary of Output File-Related Functions #include <fstream> ofstream fsOut; fsOut.open(const char[] fname) connects stream fsOut to the external file fname. fsOut.put(char c) inserts character c to the output stream fsOut. fsOut.eof() tests for the end-of-file condition. fsOut.close() disconnects the stream and associated file. fsOut << c; //Behaves just like cout