File IO - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

File IO

Description:

A file is a named area in secondary storage that holds a collection of information. Each ... readme.txt program.exe. lab7.cpp LAB7.CPP. iostream.h report.doc ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 13
Provided by: bridges
Category:
Tags: file | readme

less

Transcript and Presenter's Notes

Title: File IO


1
File I/O
  • Reading Chapter 3

2
File
  • A file is a named area in secondary storage that
    holds a collection of information
  • Each file has a name and extension (optional)
  • Name can usually be at least eight characters
    long, and usually alpha-numeric characters
  • Extension is usually up to three characters long,
    and used to group files by type
  • Some operating systems are case-sensitive

readme.txt program.exe lab7.cpp
LAB7.CPP iostream.h report.doc
3
Why file I/O?
  • Programs can
  • Read data from files instead of keyboard
  • Write output to files instead of screen
  • Advantages of file input
  • Easier to enter data into a file
  • Data does not need to be retyped
  • Advantages of file output
  • Output can be seen over and over again without
    rerunning the program
  • The output of one program can be used as input to
    another

4
C file streams
  • C allows file I/O through file stream objects

5
C file streams
  • The C I/O library defines
  • Input file stream (ifstream) stream of
    characters coming into a program from an input
    file
  • Output file stream (ofstream) stream of
    characters going from a program to an output file

6
Performing file I/O
  • Include the fstream header file
  • include ltfstreamgt
  • Declare file stream objects
  • Open files using file stream objects
  • Use file stream objects to read data from a file
    or write data into a file (in the same way cin or
    cout is used)
  • Close files

7
Declaring file stream objects
  • Declaring file stream objects without file names
  • ifstream inFile // input file stream
  • ofstream outFile // output file stream
  • Declaring file stream objects with file names
  • ifstream inFile ("input.dat")
  • ofstream outFile ("output.dat")

8
Opening a file
  • Files may be opened like
  • ifstream inFile
  • ofstream outFile
  • inFile.open ("input.dat")
  • outFile.open ("output.dat")

9
What does a file opening do?
  • Associates the file stream object with the
    physical (disk) name of the file
  • If the input file does not exist on disk, open is
    not successful
  • If the output file does not exist on disk, a new
    file with that name is created
  • If the output file already exists, it is
    overwritten
  • Places a file reading marker at the very
    beginning of the file, pointing to the first
    character in it

10
Reading and writing with file streams
  • Read using the gtgt operator
  • inFile gtgt aNumber
  • infile gtgt stringVariable gtgt aNumber
  • Write using the ltlt operator
  • outFile ltlt "Output to the file" ltlt endl
  • outFile ltlt aNumber

11
Closing a file
  • Files opened with statements like
  • ifstream inFile("input.dat")
  • ofstream outFile
  • outFile.open ("output.dat")
  • are closed with
  • inFile.close()
  • outFile.close()

12
Assignment
  • Write a C program that will read the data from
    an input file called data.in and display the data
    in the screen.
Write a Comment
User Comments (0)
About PowerShow.com