130 likes | 222 Views
COMP 102 Programming Fundamentals I. Presented by : Timture Choi. File I/O. File External collection of data Provided input to a program Stored output from a program Stored on secondary storage permanently Must be Opened before used Closed after used File I/O
E N D
COMP 102Programming Fundamentals I Presented by : Timture Choi
File I/O • File • External collection of data • Provided input to a program • Stored output from a program • Stored on secondary storage permanently • Must be • Opened before used • Closed after used • File I/O • Operation related to file
Stream • Sequence of characters • <iostream> – user interactive • cin • Input stream associated with keyboard • cout • Output stream associated with display • <fstream> – file operation • ifstream • Defined input stream associated with file • ofstream • Defined output stream associated with file
File Stream • Before • Getting data from a file • Output data to a file • Declare a stream object • Creating a channel • Associate this stream object to the file • Connecting the stream component to the file with program
File Stream • E.g. #include <fstream> … ifstream fin; … fin.open("filename.txt"); // connects stream fsIn to the external // file "filename.txt" … fin.close(); // disconnects the stream and associated file …
Input File • Include the <fstream> • #include <fstream> • Declare an input stream object • ifstream fin; • Open an dedicated file • fin.open(“filename.txt”); • Get character from file • fin.get(character); • Close the file • fin.close();
Output File • Include the <fstream> • #include <fstream> • Declare an output stream object • ofstream fout; • Open an dedicated file • fout.open(“filename.txt”); • Put character from file • fout.put(character); • Close the file • fout.close();
File I/O – Function • Input file • fin.eof() • Test for end-of-file condition • fin.get(char& character) • Obtains the next character from the file stream • Put into the variable character • Output file • fout.put(char character) • Inserts character to the output file stream
File I/O: More Function • E.g. … ifstream fin; char character; fin.open(“data.txt”); // open the file fin.get(character); // get the first char while(!fin.eof()){ // loop to read each character … fin.get(character); … } fin.close(); …
Stream Manipulators • Used to manage the input and output format of a stream • With the directive <iomanip> • E.g. • cout << endl; • cout << hex; • cout << setw(5);
Stream Manipulators • #include <iomanip> • setprecision(d) • Set number of significant digits tod • setw(w) • Set field width tow • setfill(c) • Set fill character toc • setiosflags(f) • Set flagfto 1 • resetiosflags(f) • Set flagfto 0
Stream Manipulators • E.g. … int a = 1234567; int b = 10; … cout << hex << a << endl; … cout << setiosflags(ios::showpos) << b << endl; cout << resetiosflags(ios::showpos); … cout << setiosflags(ios::scientific) << b << endl; …
SUMMARY • By the end of this lab, you should be able to: • Read/Write/Manipulate file with directive • <fstream> • Formatting input and output stream with directive • <iomanip>