200 likes | 391 Views
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).
E N D
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)
Redirection isn't enough What if we wanted to read from more than one file at the same time? student registration file prepare bill -> tuition bill student info file
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.
How to use file streams • Include <fstream> • Declare input files as ifstream • Declare output files as ofstream • To read in, use >> • To write out use << • Keep going until eof is reached
#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; }
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; }
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)
Passing file streams as parameters • Always pass as reference • type is ifstream or ofstream void getnextword(ifstream&, string&); getnextword(infile, word); void getnextword(ifstream &infile, string &word) { if (!infile.eof()) infile >> word; }
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)
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?
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.
cerr Original 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; }
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
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)
Output flags instead 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
Output flags/manipulators • Lots of them: • right, left, internal • fill, setfill • hex, octal, dec, showbase • uppercase • scientific, fixed, showpoint • precision • showpos I will not expect you to memorize these for a test; know they exist so you can look them up as needed
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
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
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