Chapter 15.1 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Chapter 15.1

Description:

File (data file) A collection of data, stored under a common ... Reopen the file for output */ file1.open ('grades.dat', ios::out); file1 HW; file1.close ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 13
Provided by: marguerite1
Category:
Tags: chapter | reopen

less

Transcript and Presenter's Notes

Title: Chapter 15.1


1
Chapter 15.1 Files for input and output
Lets cut down on keyboard input! Can we save
the output?
2
Definitions
  • File (data file) A collection of data, stored
    under a common name, on a storage medium other
    than main memory
  • Examples C programs, Saved messages
  • File Stream a one-way transmission path, used
    to connect a file stored on a device (like a
    disk) to a program
  • Mode determines the direction of data on the
    transmission path
  • Input file stream reads data from a file
  • Output file stream writes data to a file

3
Input and Output file streams
Program
Disk
include ltfstreamgt int main () ( return 0
Input file stream
file
Output file stream
4
File Stream Objects and Methods
  • If you want to use a file for output, declare a
    variable of ofstream type
  • ofstream output file streams
  • ofstream Outfile
  • ofstream Out
  • If you want to use a file for input, declare a
    variable of ifstream type
  • ifstream input file streams
  • ifstream Infile
  • ifstream In
  • Any variable name can be used as long as it
    conforms to Cs identifier rules

5
File Stream methods
  • Prewritten functions
  • Connecting a stream to an external file name
    opening a file
  • Infile.open (Student.data, ios in)
  • Determining whether a successful connection has
    been made
  • if (Infile.fail( ))
  • Closing a connection
  • Infile.close ( )

6
Syntax for Output Files
  • include ltofstreamgt
  • Declare Outfile as an object of type fstream
  • ofstream Outfile
  • Open the external file with the statement
  • Outfile.open (Report.out,iosout)
  • Outfile is name in program, Report.out is
    external filename
  • Write to the file
  • Outfile ltlt Grade\t ltlt grade
  • (writes to the file instead of the screen)
  • use Outfile instead of cout (start/debug with
    cout)
  • Close the file
  • Outfile.close()

7
Syntax for Input Files
  • include ltifstreamgt
  • Declare Infile as an object of type ifstream
  • fstream Infile
  • Open the external file with the statement
  • Infile.open (Student.data,iosin)
  • Infile - name in program, Student.data - external
    filename
  • Read from the file
  • Infile gtgt grade
  • (reads from the file instead of from the
    keyboard)
  • use Infile instead of cin (Make sure file
    exists!)
  • Close the file
  • Infile.close()

8
Mode Indicators
  • ios in
  • ios out
  • ios app
  • ios ate
  • ios binary
  • ios trunc
  • ios nocreate
  • ios noreplace
  • open in input mode
  • open in output mode
  • open in append mode
  • go to end of opened file
  • open in binary mode (default, text)
  • Delete file contents, if it exists
  • If file does not exist, open fails
  • If file exists, open for output fails

9
                                                                                                                                                                                    
      
10
Same file for input and output
  • include ltiostreamgt
  • include ltfstreamgt
  • Using namespace std
  • main ()
  • fstream file1
  • int HW, newgrade
  • System(clear)
  • / Open the file for input /
  • file1.open ("grades.dat", iosin)
  • cout ltlt "Homework total is "
  • file1 gtgt HW // Print score that was read from
    file
  • cout ltlt HW ltlt '\n'
  • file1.close () // Close input file

11
Keep Value Around Between Runs
  • / Add score from keyboard /
  • cout ltlt "What is score to be added?"
  • cin gtgt newgrade
  • cout ltlt '\n'
  • HW newgrade
  • cout ltlt "Homework total is now "
  • cout ltlt HW ltlt '\n'
  • / Reopen the file for output /
  • file1.open ("grades.dat", iosout)
  • file1 ltlt HW
  • file1.close()
  • return 0
  • //Run it again, new score is there!

12
Read until End of File (EOF)
  • void main (void)
  • fstream TestFile
  • TestFile.open ("loop.file", iosin)
  • int TestScore, ID, Zip
  • // Read first record
  • TestFile gtgt TestScore gtgt ID gtgt Zip
  • while ( TestFile.eof() 0)
  • //same as (! TestFile.eof() )
  • // Process record
  • Cout ltlt TestScore ltlt ID ltlt Zip
  • // Read next record
  • TestFile gtgt TestScore gtgt ID gtgt Zip
  • // end while
  • TestFile.close() // end main
Write a Comment
User Comments (0)
About PowerShow.com