> fname; // user outfile.open(fname.c_str());// Convert to c-style string"> > fname; // user outfile.open(fname.c_str());// Convert to c-style string">
Download presentation
Presentation is loading. Please wait.
Published byJonathan Barton Modified over 11 years ago
1
Files Used to transfer data to and from disk
2
Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable outfile.open("myfile");// Open stream with file name
3
User-Supplied File Name #include // File stream library #include "apstring.h". ofstream outfile;// Declare file stream variable apstring fname;// Variable for file name cout << "Enter the output file name: "; // Get file name from cin >> fname; // user outfile.open(fname.c_str());// Convert to c-style string
4
Output Pattern with << #include // File stream library. ofstream outfile;// Declare file stream variable outfile.open("myfile");// Open stream with file name for (int i = 1; i <= 10; ++i) // Output integers 1-10 outfile << i << endl; // One integer per line of text outfile.close(); // Close stream after all output
5
Format Manipulators #include // File stream library #include. ofstream outfile;// Declare file stream variable outfile.open("myfile");// Open stream with file name outfile << setiosflags(ios::fixed | ios::showpoint) << setprecision(2); for (int i = 1; i <= 10; ++i) // Output reals with two outfile << sqrt(i) << endl; // figures of precision outfile.close(); // Close stream after all output
6
Opening an Input File Stream #include // File stream library. ifstream infile;// Declare file stream variable infile.open("myfile");// Open stream with file name
7
Input Pattern with >> #include // File stream library. ifstream infile;// Declare file stream variable infile.open("myfile");// Open stream with file name int data; infile >> data;// Priming input while (! infile.fail()) // Test for any data there { cout << data << endl; // Echo to terminal screen infile >> data; // Attempt another input } infile.close(); // Close the stream
8
A Shortened Form #include // File stream library. ifstream infile;// Declare file stream variable infile.open("myfile");// Open stream with file name int data; while (infile >> data) // Input and test cout << data << endl; // Echo to terminal screen infile.close(); // Close the stream
9
> with Files As with terminal I/O, > work with –int –double –char –apstring
10
String Input with getline The operator >> skips white space characters and does not read an entire line of text As in keyboard input, we can use getline to read a line of text as a string
11
String Input with getline #include // File stream library. ifstream infile;// Declare file stream variable infile.open("myfile");// Open stream with file name int data; while (infile >> data) // Input and test cout << data << endl; // Echo to terminal screen infile.close(); // Close the stream
12
Input Pattern with getline #include // File stream library. ifstream infile;// Declare file stream variable infile.open("myfile");// Open stream with file name apstring data; while (! infile.fail()) // Test for any data there { getline(infile, data); // Read to end of line cout << data << endl; // Echo line to terminal } infile.close(); // Close the stream
13
Character I/O with get and put #include // File stream library. ifstream infile;// Declare file stream variable ofstream outfile infile.open("myfile");// Open stream with file name outfile.open("copy");// Open stream with file name char ch; infile.get(ch); while (! Infile.fail) // Test for any data { outfile.put(ch); // Copy to new file infile.get(ch); } infile.close(); // Close the stream
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.