CS162: Introduction to Computer Science II Streams 1 Streams A - - PDF document

cs162 introduction to computer science ii
SMART_READER_LITE
LIVE PREVIEW

CS162: Introduction to Computer Science II Streams 1 Streams A - - PDF document

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


slide-1
SLIDE 1

1

1

CS162: Introduction to Computer Science II

Streams

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

slide-2
SLIDE 2

2

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)

  • fstream (for output streams)

4

slide-3
SLIDE 3

3

Writing to a file

5

Writing to a file

#include <fstream> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt");
  • utStream << "These are not the droids you

are looking for" << std::endl;

  • utStream.close();

return 0; }

6

slide-4
SLIDE 4

4

Writing to a file

#include <fstream> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt");
  • utStream << "These are not the droids you

are looking for" << std::endl;

  • utStream.close();

return 0; }

7

  • This associates the file output.txt with

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

Writing to a file

#include <fstream> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt");
  • utStream << "These are not the droids you

are looking for" << std::endl;

  • utStream.close();

return 0; }

8

  • Notice how everything is done

through outStream

  • Use << to send output to a file (just

like you do with cout)

slide-5
SLIDE 5

5

Writing to a file

#include <fstream> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt");
  • utStream << "These are not the droids you

are looking for" << std::endl;

  • utStream.close();

return 0; }

9

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

Writing to a file

  • Recall that the line below overwrites output.txt

if it exists

  • utStream.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:

  • utStream.open(“output.txt”,

std::ios::app);

10

slide-6
SLIDE 6

6

Writing to a file

#include <fstream> #include <iostream> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt",std::ios::app);
  • utStream << "These are not the droids you

are looking for" << std::endl;

  • utStream.close();

return 0; }

11

Writing to a file

  • If you run the filewriter program twice,

you end up with output.txt containing:

12

These are not the droids you are looking for These are not the droids you are looking for

slide-7
SLIDE 7

7

Writing to a file

  • Instead of two separate statements
  • fstream outStream;
  • utStream.open(“output.txt”,std::ios:app);
  • You can combine them:
  • fstream outStream(“output.txt”,std::ios::app);
  • Output is buffered, meaning << doesn’t write

right away. You can force a write using flush

  • utstream.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

slide-8
SLIDE 8

8

Writing to a file

#include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt",std::ios::app);

if( outStream.fail() ) { std::cout << "Output file opening failed.\n"; exit(1); }

  • utStream << "These are not the droids you are looking

for" << std::endl;

  • utStream.close();

return 0; }

15

Writing to a file

#include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt",std::ios::app);

if( outStream.fail() ) { std::cout << "Output file opening failed.\n"; exit(1); }

  • utStream << "These are not the droids you are looking

for" << std::endl;

  • utStream.close();

return 0; }

16

  • Need this for the

exit() function

slide-9
SLIDE 9

9

Writing to a file

#include <fstream> #include <iostream> #include <cstdlib> int main(int argc, char** argv) { std::ofstream outStream;

  • utStream.open("output.txt",std::ios::app);

if( outStream.fail() ) { std::cout << "Output file opening failed.\n"; exit(1); }

  • utStream << "These are not the droids you are looking

for" << std::endl;

  • utStream.close();

return 0; }

17

  • exit(1) means your program exited

with some error condition

  • exit(0) means your program exited

without any errors

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

slide-10
SLIDE 10

10

Formatting output

Examples:

  • Prints floating point numbers in everyday

notation (not exponential notation)

  • utStream.setf(ios::fixed);
  • Print the decimal point
  • utStream.setf(ios::showpoint);
  • Puts 2 digits after the decimal point
  • utStream.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

Prints out: 10 20 (each number takes up 4 spaces)

20

Must be called each time

slide-11
SLIDE 11

11

Formatting output

  • setprecision( ) sets the precision

cout << setprecision(2) << 10 << “ “ << 20 << endl;

Prints out: 10.00 20.00

21

Take effect and stays in effect

Formatting Output

  • You can set multiple flags with a bitwise OR
  • perator | eg.
  • utStream.setf(ios::fixed |

ios::showpoint | ios::right);

  • You can save your flags

long flagSettings =

  • utStream.flags();
  • You can set your flags:
  • utStream.flags(flagSettings)
  • You can unset a flag using unsetf(flag)
  • n the output stream

22

slide-12
SLIDE 12

12

Reading from a file

23

Reading from a file

  • Suppose I have a file called input.txt

with the following lines:

24

1 2 3

slide-13
SLIDE 13

13

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()) { 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(); }

26

  • input.txt now associated with inStream.
  • Note: open() only accepts C-style

strings and not string objects. Use c_str() on a string object to get the C- style string

slide-14
SLIDE 14

14

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; inStream.close(); }

27

Opening a file for reading can fail eg. if it doesn’t exist

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; inStream.close(); }

28

>> used to read in each line. Note: this assumes there are only 3 lines in the file input.txt.

slide-15
SLIDE 15

15

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

slide-16
SLIDE 16

16

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); } std::string next; while(inStream >> next) { std::cout << next << " " << std::endl; } inStream.close(); }

31

Reading in from a file

  • Instead of two lines:

ifstream inStream; inStream.open(“input.txt”);

  • You can use one line:

ifstream inStream(“input.txt”);

32