Computational Intelligence on Automation Lab @ NCTU UEE1302 (1102) F10 Introduction to Computers and Programming (I) Programming Lecture 09 File I/O & Data Manipulation I/O Streams File I/O Learning Objectives Character I/O Tools for Stream I/O File names as input Formatting output, flag settings Stream Hierarchies Preview of inheritance Random Access to Files PRO_09 HUNG-PIN(CHARLES) WEN 2 Introduction to Streams What are streams? a transfer of information in the form of a sequence of bytes Special objects Deliver program input and output Input stream: flow into program Can come from keyboard or file Output stream: flow out of program Can go to screen or file Seen streams already!! <iostream> cin: input stream connected to keyboard cout: output stream connected to screen PRO_09 HUNG-PIN(CHARLES) WEN 3 Illustration of <iostream> Standard streams are created, connected, and disconnected automatically by OS. cin, cout, cerr, clog : behaves like a text file. buffered un-buffered PRO_09 HUNG-PIN(CHARLES) WEN 4
Streams Like cin & cout Given program defines stream instream that comes from some file: int number; instream >> number; Reads value from stream, assigned to variable number Program defines stream outstream that goes to some file outstream << the number is << number; Writes value to stream, which goes to file Program defines stream to come from and go to some file concurrently. PRO_09 HUNG-PIN(CHARLES) WEN 5 Illustration of File Streams input stream output stream input/output stream PRO_09 HUNG-PIN(CHARLES) WEN 6 Files File: collection of data that is stored together under common name on storage media C++ sources as text files on hard disks Reading from file When program takes input Writing to file When program sends output Start at beginning of file to end Other methods available We ll discuss this simple text file access here PRO_09 HUNG-PIN(CHARLES) WEN 7 File Names Files have two names to our programs External filename Also called physical filename Example: "input_file.txt" Sometimes considered real filename Used only once in program (to open) Stream name Also called logical filename Example: infile in "ifstream infile;" C++ program uses this name for all file activity PRO_09 HUNG-PIN(CHARLES) WEN 8
File Connection & File I/O Must first connect file to stream object For input: file ifstream object For output: file ofstream object Use ifstream and ofstream classes #include <fstream> using namespace std; Defined in library <fstream> Named in std namespace Alternative form #include <fstream> using std::ifstream; using std::ofstream; PRO_09 HUNG-PIN(CHARLES) WEN 9 Declaring Streams Stream must be declared like any other class variable: ifstream infile; ofstream outfile; Must then connect to file: infile.open( input_file.txt ); opening the file by member function open() can specify complete pathname filename must be c-strings Can specify filename at declaration ifstream infile( input_file.txt ); ofstream outfile( output_file.txt ); PRO_09 HUNG-PIN(CHARLES) WEN 10 File Streams Usage Once declared use normally! int onenumber, anothernumber; infile >> onenumber >> anothernumber; Output stream similar: ofstream outfile; outfile.open.open( output_file.txt ); outfile << onenumber = << onenumber << anothernumber = << anothernumber; Send items to output file flag ios::in Open File Stream w/ Flags Explanation input state; default for input file ios::out output state; default for output file ios::app Append output state. ios::ate Position file marker at the end of file ios::trunc Delete all data from an existing file when it is opened. Default for output file. ios::binary Binary file; default is text file. PRO_09 HUNG-PIN(CHARLES) WEN 11 PRO_09 HUNG-PIN(CHARLES) WEN 12
Closing Files Files should be closed When program completed getting input or sending output Disconnects stream from file Example: instream.close(); outstream.close(); no arguments Files automatically close when program ends Good to close opened files explicitly File Flush Output often buffered Temporarily stored before written to file Written in groups Occasionally might need to force writing: outstream.flush(); Member function flush, for all output streams All buffered output is physically written Closing file automatically calls flush() PRO_09 HUNG-PIN(CHARLES) WEN 13 PRO_09 HUNG-PIN(CHARLES) WEN 14 Appending to a File Checking File Open Success Standard open operation begins with empty file Even if file exists contents lost Open for append: ofstream outfile; outfile.open( output.txt,ios::app); If file doesn t exist creates it If file exists appends to end 2 nd argument is class ios defined constant in <iostream> library, std namespace PRO_09 HUNG-PIN(CHARLES) WEN 15 File opens could fail If input file doesn t exist No write permissions to output file Unexpected results Place call to.fail() or.is_open() to check stream operation success instream.open( stuff.txt ); if (instream.fail()) { cout << "File open failed.\n"; exit(1);.is_open() returns the opposite.fail().fail() PRO_09 HUNG-PIN(CHARLES) WEN 16
Checking End-of of-file w/ eof() Use loop to process file until end two ways to test for the end of file Use member function eof() instream.get(next); while (!instream.eof()) { cout << next; instream.get(next); Reads each character until file ends eof() member function returns bool Member function get() comes soon! PRO_09 HUNG-PIN(CHARLES) WEN 17 Checking End-of of-file w/ Read Second method: read operation returns bool value! a good way to read file (instream >> next) expression returns true if read successful return false if attempt to read beyond end of file In action: double next, sum = 0; while (instream >> next) sum = sum + next; cout << the sum is << sum << endl; PRO_09 HUNG-PIN(CHARLES) WEN 18 Checking File I/O Status Character I/O with Files prototype fail() eof() good() bad() description return true if the file has not been opened successfully; otherwise, return false return true if a read has been attempted past the end-of-file; otherwise, return false return true if the file is available for program use; otherwise, return false return true if a fatal error with the current stream has occurred; otherwise, false. not normally occur. All cin and cout character I/O same for files! Common character I/O functions get(), getline(): obtain characters from input file put(): put one character to output streams putback(): put back the character just read to input streams peek(): return the next character from the stream without removing it ignore(): skips over a designated number of characters PRO_09 HUNG-PIN(CHARLES) WEN 19 PRO_09 HUNG-PIN(CHARLES) WEN 20
get(): Read Characters from File get(): obtain characters from file and save it to the input stream. 3 forms: istream& get(char& ch); most suggested get() istream& get(char* buffer,streamsize num); istream& get(char* buffer,streamsize num, char delim); Example for Form 1: ifstream infile( input_file.dat, ios::in); char ichar; while (infile.get(ichar)) { cout << ichar; PRO_09 HUNG-PIN(CHARLES) WEN 21 getline() ():: Read a Line from File getline() (): read characters into input stream buffer until either: (num - 1) characters have been read, an EOF is encountered, or, until the character delim (normally, newline, \n ) is read. The delim character is not put into buffer. Two forms: istream& getline(char* buffer, streamsize num); istream& getline(char* buffer, streamsize num, char delim); PRO_09 HUNG-PIN(CHARLES) WEN 22 Examples of getline() put(): : Put One Character to File Example 1 (for C-String variables): int MAX_LENGTH = 100; char line[max_length]; while (infile.getline(line, MAX_LENGTH)) { cout << read line: << line << endl; Example 2 (for string variables): string line; while (getline(infile, line)) { cout << read line: << line << endl; PRO_09 HUNG-PIN(CHARLES) WEN 23 put(): put one character to the output stream and save it to the file Syntax: ostream& put(char ch); Example: ofstream outfile( output_file.dat ); string article = Today is Dec-30-2010.\n We still have two more lectures.\n Wish us a happy new year!\n ; for (int idx=0;idx < article.size();idx++) { outfile.put put(article[idx]); outfile.close(); PRO_09 HUNG-PIN(CHARLES) WEN 24
putback() ():: Put Back One Character putback() (): return the previously-read character ch to the input stream Syntax: istream& putback( char ch ); Example: // get a number or a word? ifstream infile( sample.dat ); int n = 0; char str[256]; char c = infile.get(); if ( (c >= 0 ) && (c <= 9 ) ) { infile.putback putback(c); infile >> n; // this case is a number else { infile.putback putback(c); cin >> str; //this case is a word peek(): Read/Return Next Character peek(): return the next character in the stream or EOF if the end of file is read not remove the character from the stream Example: // get a number or a word? ifstream infile( sample.dat ); int n = 0; char str[256]; char c = infile.peek peek(); if ( (c >= 0 ) && (c <= 9 ) ) { infile >> n; // this case is a number else { cin >> str; //this case is a word PRO_09 HUNG-PIN(CHARLES) WEN 25 PRO_09 HUNG-PIN(CHARLES) WEN 26 ignore(): : Skip Characters Random File Access (1/3) ignore(): read and throw away characters until num characters have been read or until the character delim is read Syntax: istream& ignore(streamsize num=1, int delim=eof); Example: char first, last; cout << Enter your first and last names: ; first = infile.get(); infile.ignore ignore(256, ); last = infile.get(); cout <<"Your name is " << first << last; PRO_09 HUNG-PIN(CHARLES) WEN 27 Sequential file organization: characters in file are stored in sequential manner Random Access: any character in an opened file can be read directly without having to read characters ahead of it File position marker: long integer that represents an offset from the beginning of each file Keep track of where next character is to be read from or written to Allow for random access of any individual character PRO_09 HUNG-PIN(CHARLES) WEN 28
Random File Access (2/3) Random File Access (3/3) Finding record 101 using sequential access: One file = a sequential stream of n characters 0 1 2 100 101 102 ios:beg ios:cur ios:end Finding record 101 using random access: 0 1 2 100 101 102 ios:beg ios:cur ios:end PRO_09 HUNG-PIN(CHARLES) WEN 29 Name seekg(offset,mode) seekp(offset,mode) tellg(void) tellp(void) description For input files, move to the offset position as indicated by the mode For output files, move to the offset position as indicated by the mode For input files, return the current value of the file position marker For output files, return the current value of the file position marker Types of modes: ios::beg: the beginning of the file ios::cur: current position of the file ios::end: the end of the file PRO_09 HUNG-PIN(CHARLES) WEN 30 Random Access Tools (1/2) Opens same as istream or ostream fstream rwstream; rwstream.open( stuff.txt,ios::in ios::out); Adds second argument Open with read and write capability Move about in file rwstream.seekp(1000); Positions put-pointer at 1000 th byte rwstream.seekg(1000); Positions get-pointer at 1000 th byte rwstream.seekp(100*sizeof(mystrcut) 1); Position put-pointer at 100th record of objects PRO_09 HUNG-PIN(CHARLES) WEN 31 Random Access Tools (2/2) seekg() and seekp() can be used with three modes: ios::beg, ios::cur and ios::end (EX 1) infile.seekg(10, ios::beg) File position marker moves to the 10 th character from the beginning of the file (EX 2) infile.seekg(-6, ios::cur) File position marker moves back 6 characters from the current position (EX 3) outfile.seekp(0, ios::end) File position marker moves to the last characters at the end of file PRO_09 HUNG-PIN(CHARLES) WEN 32
Parse Command Line Obtain parameters from command line by int main(int argc, char *argv[]) argc: total count of the parameters including the program name argv: indexed array of c-strings Example: >./prog dog cat tiger argc is 4 argv[0] is prog argv[1] is dog argv[2] is cat argv[3] is tiger PRO_09 HUNG-PIN(CHARLES) WEN 33 Tools: File Names as Input Stream open operation Argument to open() is c-string type Can be literal (used so far) or variable string filename; ifstream infile; cout << Enter file name: ; cin >> filename; infile.open(filename.c_str()); Provides more flexibility Open file from command line argument Ex: infile.open(argv[2]); PRO_09 HUNG-PIN(CHARLES) WEN 34 Common Programming Errors (1/2) Using file s external name in place of internal file stream object name when accessing file Opening file for output without first checking that file with given name already exists Not checking for preexisting file ensures that file will be overwritten Not understanding that end of a file is detected only after EOF sentinel has either been read or passed over Common Programming Errors (2/2) Attempting to detect end of file using character variables for EOF marker Any variable used to accept EOF must be declared as an integer variable Using integer argument with the seekg() and seekp() functions Offset must be a long integer constant or variable Any other value passed to these functions can result in unpredictable result PRO_09 HUNG-PIN(CHARLES) WEN 35 PRO_09 HUNG-PIN(CHARLES) WEN 36
Summary (1/2) A data file is any collection of data stored in an external storage medium under a common name A data file is connected to file stream using open() method in <fstream> connect file s external name with internal object name A file can be opened in input or output mode An opened output file stream either creates a new data file or erases data in an existing opened file PRO_09 HUNG-PIN(CHARLES) WEN 37 Summary (2/2) All file streams must be declared as objects of either the ifstream or ofstream classes Data files can be accessed randomly using the seekg(),seekp(),tellg(),and tellp() methods g versions of these functions are used to alter and query file position marker for input file streams p versions do the same for output file streams Parse command line by int main(int argc, char *argv[]) PRO_09 HUNG-PIN(CHARLES) WEN 38