tbb1073 file part1

43
7/28/2019 TBB1073 File Part1 http://slidepdf.com/reader/full/tbb1073-file-part1 1/43 TBB1073 - SPDB Streams  Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons and Evan Gallagher, New York University AND  Absolute C++ by Savitch, Pearson, New Jersey.

Upload: cyrus-hong

Post on 03-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 1/43

TBB1073 - SPDB

Streams

 Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John

Wiley & Sons and Evan Gallagher, New York University AND

 Absolute C++ by Savitch, Pearson, New Jersey.

Page 2: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 2/43

• To be able to read and write files • To convert between strings and numbers usingstringstream s

• To understand the concepts of sequential and random

access 

Chapter Goals

Page 3: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 3/43

Streams

 A very famous bridge

over a “stream”  

Page 4: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 4/43

Streams

 A ship

Page 5: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 5/43

Streams

in the stream

Page 6: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 6/43

Streams

one at a time

Page 7: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 7/43

Streams

 A stream of ships

Page 8: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 8/43

Streams

an input stream to

that famous city

Page 9: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 9/43

Streams

Page 10: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 10/43

Streams

No more ships in the stream at this time 

Page 11: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 11/43

 

Sequential Access and Random Access

There are two methods of working with files:

Sequential Access

 – one input at a time starting at the beginning

Random Access

Random? We do not discussed this in our 

course

Page 12: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 12/43

Sequential Access and Random Access

Page 13: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 13/43

• The C++ input/output library is based on the concept of streams. 

•  An input stream is a source of data.

•  An output stream is a destination for data.

• The most common sources and destinations for data arethe files on your hard disk.

Reading and Writing Files

Page 14: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 14/43

Streams

This is a stream of characters. It could be from the keyboard or from a file. Each of these is

just a character - even these: 3 -23.73 which,when input, can be converted to: ints or doublesor whatever type you like.(that was a '\n' at the end of the last line)&*@&^#!%#$ (No, that was –not- a curse!!!!!!!!!!¥1,0000,0000 (price of a cup of coffee in Tokyo)Notice that all of this text is very plain - Nobold or green of italics – just characters – and whitespace (TABs, NEWLINES and, of course... the

other one you can’t see: the space character: (another '\n')(&& another) (more whitespace) and FINALLY:

 Aren't you x-STREAM-ly glad this animation is over?

newline characters

 And there were no sound effects!!!

Page 15: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 15/43

The stream you just saw in action is a plain text file.No formatting, no colors, no video or music

(or sound effects).

The program can read these sorts of plain text streams of characters from the keyboard, as has been done so far.

Reading and Writing Files

Page 16: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 16/43

You can also read from files stored on your hard disk:

from plain text files

(as if the typing you saw had been stored in a file)

(or you wanted to write those characters to a disk file).

from a file that has binary information

(a binary file).

Reading and Writing Files

The picture of the ship in the stream

of ships is stored as binary information.

Page 17: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 17/43

 

You will learn to read and write only sequential files.

Reading and Writing Files

Page 18: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 18/43

 To read or write disk files, you use variables.

You use variables of type:

ifstream for input from plain text files.

ofstream for output to plain text files.

fstream for input and output from binary files.

Reading and Writing Files

Page 19: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 19/43

To read anything from a file stream,you need to open the stream. 

(The same for writing.)

Opening a Stream

Page 20: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 20/43

  Opening a stream means associatingyour stream variable with the disk file.

Opening a Stream

Page 21: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 21/43

The first step in opening a file is

having the stream variable ready.

Here’s the definition of an input 

stream variable named in_file:

ifstream in_file;

Looks suspiciously like every other 

variable definition you’ve done 

 – it is!

Only the type name is new to you.

Opening a Stream

Page 22: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 22/43

Suppose you want to read data from a file namedinput.dat located in the same directory asthe program.

 All stream variables are objects so we will use amethod.

The open method does the trick:

in_file.open("input.dat");

Opening a Stream

Page 23: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 23/43

You use the name of the disk fileonly when you open the stream.

 And the open method only accepts C strings.

(More about this later …) 

Opening a Stream

Page 24: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 24/43

C++ for Everyone by Cay HorstmannCopyright © 2008 by John Wiley & Sons. All rights reserved

File names can contain directory path information, such as:

UNIX in_file.open("~/nicework/input.dat");

Windows in_file.open("c:\\nicework\input.dat");

Opening a Stream

Page 25: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 25/43

C++ for Everyone by Cay HorstmannCopyright © 2008 by John Wiley & Sons. All rights reserved

File names can contain directory path information, such as:

UNIX in_file.open("~/nicework/input.dat");

Windows in_file.open("c:\\nicework\input.dat");

Opening a Stream

When you specify the file name as a string literal,

and the name contains backslash characters

(as in a Windows path and filename),

you must supply each backslash twiceto avoid having escape characters in the string, 

like '\n'.

Page 26: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 26/43

If you ask a user for the filename, you would

normally use a string variable to store their input.

But the open method requires a C string.

What to do?

Opening a Stream

Page 27: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 27/43

Luckily most classes provide the methods we need:

the string class has the c_str method

to convert the C++ string to a C string:

Opening a Stream

cout << "Please enter the file name:";string filename;cin >> filename;ifstream in_file;

in_file.open(filename.c_str());

Page 28: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 28/43

Theopen

method is passed C string

version of the filename the user typed:

Opening a Stream

cout << "Please enter the file name:";string filename;cin >> filename;ifstream in_file;

in_file.open(filename.c_str());

Page 29: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 29/43

 

When the program ends,all streams that you have opened

will be automatically closed.

You can manually close a stream with theclose member function:

in_file.close();

Closing a Stream

Page 30: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 30/43

 

Manual closing a stream is only necessaryif you want to open the file again in

the same program run. 

Closing a Stream

Page 31: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 31/43

 

You already know how to read and write using files.

Yes you do:

string name;

double value;

in_file >> name >> value;

Reading from a Stream

cin? in_file?

No difference when it comes to reading using >> .

See, I told you so!

Page 32: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 32/43

 

The >>  operator returns a “not failed” condition, allowing you to combine an input statement and a test.

 A “failed” read yields a false 

and a “not failed” read yields a true.

if (in_file >> name >> value){

// Process input

}

Nice!

Reading from a Stream

Page 33: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 33/43

 

The open method also sets a “not failed” condition. It is a good idea to test for failure immediately:

in_file.open(filename.c_str());// Check for failure after openingif (in_file.fail()) { return 1; }

Silly user,

bad filename!

Failing to Open

Page 34: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 34/43

 

Let’s review: 

Do you know everything about writing to files?

Of course!

Writing to a Stream

Page 35: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 35/43

 

Here’s everything: 

ofstream out_file;out_file.open("output.txt");if (in_file.fail()) { return 1; }out_file << name << " " << value << endl;

out_file << "CONGRATULATIONS!!!" << endl;

Writing to a Stream

1. create output stream variable

2. open the file

3. check for failure to open

5. congratulate self!

4. write to file

Page 36: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 36/43

 

Working with File Streams

Page 37: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 37/43

 

There are more ways to read from streamvariables than you have seen before,

and there are some details you need to understand.

These follow… 

Reading Text Input

R di

Page 38: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 38/43

 

What really happens when reading a string?

string word;in_file >> word;

1. If any reading can be done at all, all whitespace is skipped(whitespace is this set: '\t' '\n' ' '). 

2. The first character that is not white space is added to

word. More characters are added until either another white

space character occurs, or the end of the file has been

reached.

Reading strings

R di

Page 39: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 39/43

Reading strings

ifstream in_file;in_file.open ("mydata.txt");

string word;in_file >> word;cout << word;

in_file.close();

Output:3458

int id;string name;in_file >> id >> name;cout << id << name;

Output:3458Mariam 

 while(in_file >> id >> name){cout << id << " " << name;cout << endl;

}

Output:3458 Mariam 8978 Chong

9999 Luxmi

R di Ch t

Page 40: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 40/43

 It is possible to read a single character 

 – including whitespace characters.

char ch;in_file.get(ch);

The get method also returns the “not failed” conditionso:

 while (in_file.get(ch)){// Process the character ch

}

Reading Characters

N t W ti Wh t Y G t t()

Page 41: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 41/43

Not Wanting What You Get  – unget()

You can look at a character after reading it

and then put it back after if you so decide.

in_file.unget();

char ch;in_file.get(ch);if (isdigit(ch)) // Is this a number? 

{// Put the digit back so that it will

// be part of the number we read 

in_file.unget();

// Read integer starting with chint n;in_file >> n;

}

R di th Wh l Fil Ch t b Ch t

Page 42: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 42/43

The get method makes it possible to process

a whole file one character at a time:

Reading the Whole File Character by Character 

char ch;in_file.get(ch);

cout << ch;

Output:U

 while (in_file.get(ch)){

cout << ch; }

UTP Vision

 A Leader in Technology Education and Centre for Creativity and Innovation.

in_file.get(ch); while (!in_file.eof()){

cout << ch;in_file.get(ch);}

OR 

R di N b f t t fil

Page 43: TBB1073 File Part1

7/28/2019 TBB1073 File Part1

http://slidepdf.com/reader/full/tbb1073-file-part1 43/43

Reading a Number from text file

char ch; while (in_file.get(ch))?

{if(isdigit(ch)) // Is this a number

cout << ch;}

Output:101997