Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream...
-
Upload
jonathan-barton -
Category
Documents
-
view
217 -
download
0
Transcript of Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream...
Files
DisketteMemory
Input file
Output file
Used to transfer data to and from disk
Opening an Output File Stream
#include <fstream.h> // File stream library
.
.
ofstream outfile; // Declare file stream variable
outfile.open("myfile"); // Open stream with file name
User-Supplied File Name
#include <fstream.h> // File stream library#include "apstring.h"
.
.
ofstream outfile; // Declare file stream variableapstring fname; // Variable for file name
cout << "Enter the output file name: "; // Get file name fromcin >> fname; // user
outfile.open(fname.c_str()); // Convert to c-style string
Output Pattern with <<
#include <fstream.h> // 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
Format Manipulators
#include <fstream.h> // File stream library#include <iomanip.h>..ofstream outfile; // Declare file stream variableoutfile.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
Opening an Input File Stream
#include <fstream.h> // File stream library
.
.
ifstream infile; // Declare file stream variable
infile.open("myfile"); // Open stream with file name
Input Pattern with >>
#include <fstream.h> // File stream library
.
.
ifstream infile; // Declare file stream variable
infile.open("myfile"); // Open stream with file name
int data;
infile >> data; // Priming inputwhile (! infile.fail()) // Test for any data there{ cout << data << endl; // Echo to terminal screen infile >> data; // Attempt another input}infile.close(); // Close the stream
A Shortened Form
#include <fstream.h> // 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
<< and >> with Files
• As with terminal I/O, << and >> work with– int– double– char– apstring
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
String Input with getline
#include <fstream.h> // 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
Input Pattern with getline
#include <fstream.h> // 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
Character I/O with get and put
#include <fstream.h> // File stream library
.
.
ifstream infile; // Declare file stream variableofstream outfileinfile.open("myfile"); // Open stream with file nameoutfile.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