Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream...

13
Files D iskette M emory Inputfile O utputfile Used to transfer data to and from disk

Transcript of Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream...

Page 1: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

Files

DisketteMemory

Input file

Output file

Used to transfer data to and from disk

Page 2: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 3: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 4: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 5: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 6: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 7: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 8: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 9: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

<< and >> with Files

• As with terminal I/O, << and >> work with– int– double– char– apstring

Page 10: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 11: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 12: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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

Page 13: Files Used to transfer data to and from disk. Opening an Output File Stream #include // File stream library. ofstream outfile;// Declare file stream variable.

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