File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

20
File streams Chapter 14.2-14.6, 12.1- 12.4, 12.6-12.8

Transcript of File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Page 1: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

File streams

Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8

Page 2: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Remember redirection? It lets you read from a file

prompt> a.out <infile >outfile

We have substituted the disk file infile for the standard input file (keyboard)

and outfile for the standard output file (display)

 

Page 3: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Redirection isn't enoughWhat if we wanted to read from more than

one file at the same time?  student registration file

prepare bill -> tuition bill

student info file

 

Page 4: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

File Streams Its really not much different than

using cin and cout. Why call it a STREAM?

All devices can be viewed simply as a stream of chars (like a river is a stream of water) whether it is an input or output stream.

Page 5: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

How to use file streams

1. Include <fstream>2. Declare input files as ifstream3. Declare output files as ofstream4. To read in, use >>5. To write out use <<6. Keep going until eof is reached

Page 6: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

#include <fstream>

#include <iostream>

#include <string>

#include <cstdlib>

int main()

{

const char * filename="/home/faculty2/lambert/Public/unixdict.txt";

string word;

ifstream wordfile(filename);

if (!wordfile) // check to see if file is there.

{cout << "Error: no file name " << filename << endl;

return EXIT_SUCCESS;

}

Page 7: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

int count=0;

while (wordfile >> word) // keep going until eof.

{

cout << "Word is " << word << endl;

count++;

}

wordfile.close();

cout << "the number of words in the file is " << count << endl;

return EXIT_SUCCESS;

}

Page 8: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Using an output file The same as modifications to

input; instead of cout, use filename

make sure you format check for problems opening (file

will be erased if there)

Page 9: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Passing file streams as parameters Always pass as reference type is ifstream or ofstreamvoid getnextword(ifstream&, string&);

getnextword(infile, word);

void getnextword(ifstream &infile, string &word){ if (!infile.eof())

infile >> word;}

Page 10: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Write your own Write a program that reads word

from a file, writes them to a duplicate file, and counts the number of words. Use a function, writeword, which has two parameters, the file to be written to and the word being written (an input parameter)

Page 11: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

More stream functions open, get, put, eof, fail, close

put and get are used for reading files a char at a time

THEY DO NOT SKIP SPACES etc! You can use cin type input ( >> ) but it

skips white space.Remember how cin does the same?

Page 12: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

cerr, section 12.2.3 What if you wanted to output an error but

the person was redirecting the output to a file?

prompt> a.out >outfile

YOU WOULDN'T SEE THE ERROR! the cerr stream is a special kind of output

which would NOT be redirected to the standard output device.

Page 13: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

cerrOriginal program that read in words and printed

them to the screen could redirect output and produce the same results as second. But if you want to see errors no matter what, use cerr:

if (!wordfile) // check to see if file is there. {

cerr << "Error: no file name " << filename << endl; return EXIT_SUCCESS; }

Page 14: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Output manipulators a function called as though it were

being output, e.g., endl setw, precision, showpoint, scientific,

fixed can be used on any output stream, cout

or file some defined in iostream; some in

iomanip (those with parameters) problems with some on our compiler.

use flags

Page 15: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Output Manipulators

{float x;cout << setw(10) << x << endl;} endl we've seen setw(10) minimum of 10 spaces (x, like

all output is right justified) (both work fine on our system)

Page 16: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Output flagsinstead of:float x;cout << fixed << precision(2) << x << endl;

use:cout.setf (ios::fixed);cout.precision(2);cout << "x" << endl;

should do the same thing, but the first creates a syntax error on our compiler

Page 17: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Output flags/manipulators Lots of them:

right, left, internal fill, setfill hex, octal, dec, showbase uppercase scientific, fixed, showpoint precision showposI will not expect you to memorize these for a

test; know they exist so you can look them up as needed

Page 18: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Input functions (get) get (can use with cin or any input stream)

cin.get(ch); // puts char in output parameter ch = cin.get() // returns the char as function

value cin.get(chararray, maxsize, <delimiter>)

// null character inserted into array as terminator

// null character remains in input stream// works like getline

Page 19: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

Input functions getline (with cin or any input stream)

null character inserted in array at end delimiter read and discarded (UNLIKE get) cin.getline(chararray, size, <delimiter>)

member of istream class getline(istream, string, <delimiter>)

string is of string class. NO dot notation

Page 20: File streams Chapter 14.2-14.6, 12.1-12.4, 12.6-12.8.

More input functions ignore

istream.ignore(); // skips over one char istream.ignore(n); // skips over n chars istream.ignore(n, '?'); // skips over n chars, or

until a question mark whichever is first' examples:

cin.ignore(); ifstream ins; ins.ignore(1000, '\n'); // throw away next

return