lect4_2

lect4_2 - Basic File I/O 1 Basic File I/O We discussed file...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Basic File I/O 1 Basic File I/O We discussed file output briefly before. Now we describe more C++ file input and output features that are very useful. To begin with, consider the following rectangle area program discussed before. The program sends some of its output to the computer screen and some to a text file. // An example using an output file // Compute the area of a rectangle with user inputs. #include <iostream> // this line for standard I/O #include <fstream> // this line for file I/O using namespace std; int main() { ofstream outfile ("myoutput.dat"); //this line for file output int length, width, area; cout << "Enter numbers for length and width:"; cin >> length >> width; area = length * width; outfile << "Area of a rectangle with "; outfile << "length = " << length << " and width = " << width; outfile << " is " << area; outfile.close(); // this line closes the file return 0; } // end of main If the user enters 12 and 6, the input and output appearing on the screen will be as follows. Output on the screen: Enter number for length and width: 12 6 ← Output sent to the file myoutput.dat : Area of a rectangle with length = 12 and width = 6 is 72 Basic File I/O 2 • The include file fstream allows us to use functions associated with output files. The name we chose for our output file, myoutput.dat , was totally arbitrary. • The program will create that file in the directory under which our program executes. You may check the contents of that file by printing it or by opening it up with your text editor. • The output to the text file does not accumulate over several runs. Each time we run a program that sends its output to the file myoutput.dat , the previous contents of the file are erased. • The output file we created using the name myoutput.dat” uses the current working directory as the location to create the file. If we wanted to create a file with the same name in a different location, we have to specify the exact pathname with the filename. For example, to create the file and open it on a different directory on drive c, we have to use the following statement. ofstream fileout("c:\\mydir\\mydocs\\myoutput.dat"); Basic File I/O 3 Just as writing output to a file, we can also read input from a file. To illustrate this, we rewrite our rectangle area program. The program uses two files, one for reading the input, and the second for writing the output. // An example using an input file and an output file // Compute the area of a rectangle with user inputs from file #include <iostream> // this line for standard I/O #include <fstream> // this line for file I/O using namespace std; int main() { ofstream outfile ("myoutput.dat"); // for output file ifstream infile ("myinput.dat"); // for input file if ( infile.fail() ) { cout << "Input file \"myinput.dat\" opening failed" << endl; return 1; } int length, width, area; infile >> length >> width; if (length > 0 && width > 0) { area = length * width; outfile << "Area of a rectangle with ";...
View Full Document

This note was uploaded on 04/07/2008 for the course CS 181 taught by Professor Satya during the Fall '08 term at Stevens.

Page1 / 16

lect4_2 - Basic File I/O 1 Basic File I/O We discussed file...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online