37
Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Embed Size (px)

Citation preview

Page 1: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Simple FilesAn Introduction

Page 2: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Introduction• Up to this point we have seen two

files: cin and cout• Each has its corresponding

operator << or >>• cin gets from the terminal and cout

puts to the terminal• cin also eats whitespace• Of course, we have more file

capabilities than that• cin and cout are iostreams

Page 3: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Disk files• cout and cin are instances of a

type called an fstream• fstream is a class• Other instances of a class may

deal with disk files• Before talking further on disk files

a simple analogy needs to be presented

Page 4: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Saltine Cracker Theory• Saltine crackers come in a sealed

tube• Each tube contains some number

of crackers put in by the factory• You are a hungry student and want

a snack• So what are the cracker operations

that we have to know about?

Page 5: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Cracker Operations• There are several operations that

need to be performed• What do you do first?• How do we eat the first cracker?• How do we eat the 15th cracker?• What happens when the bag is

empty and we reach in our hand?

Page 6: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Cracker Operations Revisited

• The following cracker operations are needed:

• Open the bag• Get a cracker• Close the bag• Detect the end of bag condition

Page 7: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Creating Crackers• At Nabisco they have a special

process for creating the tube• They open it at the bottom• Lift the crackers in on an air

stream• The bag is flexible, so they may

put in as many as they want• They must close the bag at the end• There is nothing like the end of

bag condition

Page 8: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Files and Crackers• There are two directions for file I/O

– Input and output

• There is a process where the file is opened in a particular direction– The input open does not change the

file– The output open destroys (overwrites)

what was previously there

• There are get/put or read/write operations that handle an individual record

Page 9: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

More Operations• The end of bag is actually called

end of file• It is but one of many errors that

can occur on input• Must always be considered on

input and does not exist on output• Unlike crackers, processing a file

for input does not alter it• Unlike crackers, the output file is

only constrained by disk space

Page 10: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Object hierarchy• An object oriented language has

one or more object hierarchies• Thus an class may have

descendents• These descendents maintain the

properties and methods of the ancestor

• Most of the file classes are descendents of fstream

Page 11: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Disk Files• A facet of files not illuminated by

the saltine cracker theory is the connection issue

• There are both internal and external files

• The internal file is what we call it in the program

• The external file is the disk file or screen

Page 12: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Connections• The two files have different names• The internal file must have a C++

legal name• The external file must have name

legal to the Operating System• The connection is established

when file is opened– An internal file may be connected to

nearly any external file

Page 13: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

FSTREAMS• cout and cin are instances of a

class derived from fstream• These have predefined

connections– cin to keyboard– cout to screen

• However, we can connect a file to a disk file in either direction– Read an existing file– Write to a file

Page 14: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Classes and Connections• There are three new classes that

have different connection strategies– fstream – input or output– ifstream – input only– ofstream – output only

• These have to be declared and connected to a disk file

• DevC++ uses #include <fstream> use namespace std;

Page 15: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

ofstream• An ofstream is an output disk file• Declaration example:ofstream outf(“output.dat”);

• The string is the disk file to write upon

• This may be done with two statements as well:ofstream outf;outf.open(“output.dat”);

Page 16: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Notes• Both of these declare an internal

file named outf• They also connect to an external

disk file named “output.dat”– This needs to be in the current

directory– The current directory is the one your

project is in• If the file exists it will be destroyed

and replaced• If it does not exist it will be created

Page 17: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

More notes• The string in either the open or

declaration may be a variable or a constant string

• This file name may also include directory information:outf.open (“c:\\temp\\output.dat”);

• The backslash must be doubled because it is the escape character

Page 18: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Operations on ofstreams• The operations that you can do to

outf are exactly the same as coutcout << “The answer is “ << a<<”\n”;

• You can also look at the results after the program is done by just editting the file using the editor

• The file is opened by the declaration and is closed when the file ceases to exist due to scope

• All the manipulators work as well

Page 19: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

ifstream• An ifstream is an input disk file• Declaration example:ifstream inf(“input.dat”);

• Or:ifstream inf;inf.open(“input.dat”);

Page 20: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Same old, same old• This declares a file named inf, it

will read from the disk file “input.dat”, which needs to be in the current directory– The current directory is the one your

project is in

• The operations that you can do to inf are exactly the same as cininf >> a >> b;

Page 21: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

But some differences• There is more to know about

ifstreams for a couple of reasons:– Terminal files are infinite but disk files

are not – end of file must be detected– The ability to handle whitespace is

not needed in cout or other ofstreams• It is in input streams

• End of file– Disk files must have a finite length– Eventually if you read them long

enough you come to the end of the file

Page 22: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Handling end of file• How is this detected and what

happens?• If the file has been exhausted and

you execute:• inf >> a;• It returns an error that is ignored

but the variable a is not changed• This is a fairly harmless error

unless you are in a loop that requires something in a to change

Page 23: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

File Processing Loop• The way you detect that is use the

ifstream name as a boolean– It will return true if something is left and

false if empty or error

• Hence the traditional file processing loop is:while(inf){ … inf >> a; … }

• Caused by the C determination of true and false from any kind of variable

Page 24: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Whitespace• There are several functions that

are handled with method calls other than the use of >> or <<

• Two that will be considered are:– get– getline will considered after strings

Page 25: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

The problem:• Consider

– char ch;– inf >> ch;

• ch can never be blank• The definition of >> is that any amount

of white space is skipped• This is either good or bad depending on

what you want• It is impossible to count the number of

words in a piece of text– Unable to tell where word boundaries exist

Page 26: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

The Solution• The solution is a function that does not

care about whitespace:– cin.get(ch)

• This will get one and only one character from input

• The character obtained can be any available character including:– blank– newline– tab– any printable character

Page 27: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

End of file glitches• We do have something of a problem

with reading numbers off of an ASCII text file

• After we read the last number we have a conflict:– There are no numbers to read– The file is not at end of file

• Each compiler will do something a little different when it attempts a read of an integer but there is no integer to read– It may leave the integer unchanged– It may leave a wild value in it

• It always leaves the file in an error state

Page 28: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Three tries• What follows are three code

fragments that attempt to sum numbers off of a file

• They will also count numbers read in

• Enables computation of an average

Page 29: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

First Attemptsum = 0;

count = 0;

while(inf) {inf >> val;sum += val;count++}

• This will not work because we will read a false value or the last value on the last time through the loop

Page 30: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Second Attemptsum = 0;count = 0;while(inf) {inf >> val; if(!inf) break;sum += val;count++}

• This will work because we will test the file before dealing with the values

• Substituting a true for inf in the while shows this as a N½ loop

Page 31: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Third Attempt• The coolest wayfor(count=0,sum=0;inf>>val;

count++,sum+=val);• This will work but is somewhat harder to

read• The statement:inf>>valReads the value and puts it into val

• A side effect is that it returns the current value in inf, that is whether it is valid or not

• We use the comma operator to do all the rest that we need so the for has no body

Page 32: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Open and close• Since ifstreams and ofstreams are

both objects there are some other methods that are available

• open opens a file (prepares it for use)

• close closes it• A single internal file may touch any

number of external files– It may be closed and reopened any

number of times

Page 33: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

Open• This is a member function, so it must be

separated by a dot from the object name– outf.open(“data.fil”);

• Usually you only use open if you want to close and then reopen the file

• Close– Similar to open– File must be open in order for close to work– Will be closed automatically when program

done or when the scope is exited

Page 34: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Copyright © 2006-2014 Curt Hill

C and C++• The way C handled files is

somewhat different• It use scanf and printf and

variations• These are still available• However, they are type insecure • The fstreams are type secure• This is from the operator

overloading mechanism

Page 35: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Implementation issues• Generally it is better to close off a

file by having it drop out of scope• A close/open combination may fail

if the file was in an error state• In general if the file has problems

nothing works on it

Copyright © 2006-2014 Curt Hill

Page 36: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Using Scope

Copyright © 2006-2014 Curt Hill

// Instead of this code:ifstream inf(fileName);while(…){…} // process fileinf.close();inf.open(filename);while(…){…} // process file again// do this code:{ifstream inf(fileName);while(…){…} // process file}{ifstream inf(fileName);while(…){…} // process file again}

Page 37: Copyright © 2006-2014 Curt Hill Simple Files An Introduction

Test methods• There are several methods that

can be used to indicate file state:• bool good();• bool bad();• bool eof();• bool fail();

Copyright © 2006-2014 Curt Hill