CS162: Introduction to Computer Science II Streams 1 Streams • A stream is a flow of data • Input stream: a stream going into your program (eg. from a keyboard or file) – cin is an input stream from the keyboard • Output stream: a stream going out of your program (eg. to the screen or file) – cout is an output stream to the screen 2 1
File I/O • We will be using text files • You can create a text file using a text editor like vi, emacs, vim or notepad • If you take input from a file, you are reading from it • If you send output to a file, you are writing to it 3 File I/O • If you want to work with file I/O, you need the following include: #include <fstream> • The two stream types you will work with are: ifstream (for input streams) ofstream (for output streams) 4 2
Writing to a file 5 Writing to a file #include <fstream> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt"); outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); return 0; } 6 3
Writing to a file #include <fstream> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt"); outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); • This associates the file output.txt with return 0; the output stream outStream. } • The file output.txt is created in the same directory as the executable. • If output.txt doesn’t exist, it is created. If it already exists, it is overwritten 7 Writing to a file #include <fstream> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt"); outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); return 0; • Notice how everything is done } through outStream • Use << to send output to a file (just like you do with cout) 8 4
Writing to a file #include <fstream> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt"); outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); return 0; } Closing the file does two things: • Prevents your file from being corrupted • If you want to read this file in the near future, you need to close it 9 Writing to a file • Recall that the line below overwrites output.txt if it exists outStream.open("output.txt"); • If you want to append to the end of output.txt, do the following: – Add #include <iostream> – Change the call to open to be: outStream.open(“output.txt”, std::ios::app); 10 5
Writing to a file #include <fstream> #include <iostream> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt",std::ios::app); outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); return 0; } 11 Writing to a file • If you run the filewriter program twice, you end up with output.txt containing: These are not the droids you are looking for These are not the droids you are looking for 12 6
Writing to a file • Instead of two separate statements ofstream outStream; outStream.open(“output.txt”,std::ios:app); • You can combine them: ofstream outStream(“output.txt”,std::ios::app); • Output is buffered, meaning << doesn’t write right away. You can force a write using flush outstream.flush() 13 Writing to a file Sometimes opening a file for output can fail. Why? Lots of possibilities: • Not enough disk space • No write permissions Need to check for file open failure (see next slide). 14 7
Writing to a file #include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt",std::ios::app); if( outStream.fail() ) { std::cout << "Output file opening failed.\n"; exit(1); } outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); return 0; 15 } Writing to a file #include <fstream> #include <iostream> #include <cstdlib> • Need this for the int main(int argc, char** argv) { exit() function std::ofstream outStream; outStream.open("output.txt",std::ios::app); if( outStream.fail() ) { std::cout << "Output file opening failed.\n"; exit(1); } outStream << "These are not the droids you are looking for" << std::endl; outStream.close(); return 0; 16 } 8
Writing to a file #include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ofstream outStream; outStream.open("output.txt",std::ios::app); if( outStream.fail() ) { std::cout << "Output file opening failed.\n"; exit(1); } • exit(1) means your program exited outStream << "These are not the droids you are looking with some error condition for" << std::endl; • exit(0) means your program exited outStream.close(); return 0; without any errors 17 } Formatting output Formatting output • Sometimes you need to output text to a file with special formatting • For example, how would you like to print the number 2? 2, 2.0, 2.00, 2 • Use the setf() function on the outStream before using << 18 9
Formatting output Examples: • Prints floating point numbers in everyday notation (not exponential notation) outStream.setf(ios::fixed); • Print the decimal point outStream.setf(ios::showpoint); • Puts 2 digits after the decimal point outStream.precision(2); • See pg 536 for more formatting flags 19 Formatting output Manipulators can also be used to format output eg. • setw( ) sets the width cout << setw(4) << 10 << setw(4) << 20 Must be called each time Prints out: 10 20 (each number takes up 4 spaces) 20 10
Formatting output • setprecision( ) sets the precision cout << setprecision(2) << 10 << “ “ << 20 << endl; Take effect and stays in effect Prints out: 10.00 20.00 21 Formatting Output • You can set multiple flags with a bitwise OR operator | eg. outStream.setf(ios::fixed | ios::showpoint | ios::right); • You can save your flags long flagSettings = outStream.flags(); • You can set your flags: outStream.flags(flagSettings) • You can unset a flag using unsetf(flag) on the output stream 22 11
Reading from a file 23 Reading from a file • Suppose I have a file called input.txt with the following lines: 1 2 3 24 12
Reading from a file #include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ifstream inStream; inStream.open("input.txt"); if( inStream.fail()) { std::cout << "Input file opening failed.\n"; exit(1); } int first, second, third; inStream >> first >> second >> third; std::cout << first << " " << second << " " << third << std::endl; inStream.close(); 25 } Reading from a file int main(int argc, char** argv) { std::ifstream inStream; inStream.open("input.txt"); if( inStream.fail()) { • input.txt now associated with inStream. std::cout << "Input file opening failed.\n"; • Note: open() only accepts C-style exit(1); strings and not string objects. Use } c_str() on a string object to get the C- int first, second, third; style string inStream >> first >> second >> third; std::cout << first << " " << second << " " << third << std::endl; inStream.close(); } 26 13
Reading from a file int main(int argc, char** argv) { std::ifstream inStream; inStream.open("input.txt"); if( inStream.fail()) { std::cout << "Input file opening failed.\n"; Opening a file for reading can exit(1); fail eg. if it doesn’t exist } int first, second, third; inStream >> first >> second >> third; std::cout << first << " " << second << " " << third << std::endl; inStream.close(); } 27 Reading from a file int main(int argc, char** argv) { std::ifstream inStream; inStream.open("input.txt"); if( inStream.fail()) { std::cout << "Input file opening failed.\n"; exit(1); } int first, second, third; inStream >> first >> second >> third; std::cout << first << " " << second << " " << third << std::endl; >> used to read in each line. Note: this assumes inStream.close(); there are only 3 lines in the file input.txt. } 28 14
Reading from a file • Suppose you didn’t know how many lines were in input.txt • How do you read from a file and stop when you hit the end of the file? 29 Reading from a file • The extraction operator (instream >> next) returns: – true if there is more stuff to read and reads it into the variable next – false if you reached the end of the file and nothing is read into next 30 15
Recommend
More recommend