1 / 20

File streams

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).

ossie
Download Presentation

File streams

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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)

  3. 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

  4. 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.

  5. 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

  6. #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; }

  7. 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; }

  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)

  9. 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; }

  10. 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)

  11. 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?

  12. 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.

  13. 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; }

  14. 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

  15. 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)

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

More Related