Computing Fundamentals with C - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Computing Fundamentals with C

Description:

Extract 3 integers from the file 'input.dat' inFile n1 n2 n3; ... We often need to extract data from files stored on a computer disk. ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 19
Provided by: rickmercer9
Category:

less

Transcript and Presenter's Notes

Title: Computing Fundamentals with C


1
C and Object-Oriented Programming File I/O
2
File Streams
  • Goals
  • Use ifstream objects for disk file input
  • Use ofstream objects for disk file output
  • Apply the indeterminate loop pattern to process
    data until end of file

3
ifstream objects
  • ifstream objects
  • allow input from a disk file
  • are similar to istream objects cin
  • they share the same named operations
  • they use the same operator for input ltlt
  • must be initialized by the programmer
  • can be tested to determine if a disk file
    actually exists

4
ifstream objects continued
  • General form to construct ifstream objects
  • ifstream object-name ( "file-name" )
  • Example code
  • ifstream inFile("myfile.dat")
  • this example associates the object name inFile
    with the file named myfile.dat in the present
    working directory.

5
Reading 3 numbers
  • include ltfstreamgt // for the ifstream class
  • include ltiostreamgt // needed for cout
  • using namespace std
  • int main()
  • int n1, n2, n3
  • ifstream inFile("input.dat")
  • // Extract 3 integers from the file "input.dat"
  • inFile gtgt n1 gtgt n2 gtgt n3
  • cout ltlt "n1 " ltlt n1 ltlt endl
  • cout ltlt "n2 " ltlt n2 ltlt endl
  • cout ltlt "n3 " ltlt n3 ltlt endl
  • return 0

6
Getting the path right
  • It is easy to initialize an ifstream object and
    not have it associated with an actual disk file
  • perhaps the file does not exist
  • perhaps the path is wrong
  • Recall absolute and relative addressing concepts

7
Reading File names
  • When the user enters the file name, they may use
    one \ in UNIX it doesnt matter
  • string filename
  • cout ltlt "Enter file name "
  • cin gtgt filename
  • ifstream inFile( filename.c_str() )
  • Dialogue
  • Enter file name in.dat
  • The ifstream class needs a C string literal like
    "in.dat", or the c_str() of the string as in
  • filename.c.str() // The C string literal

8
The Indeterminate loop pattern Applied to Disk
Files
  • The end of file event can be used to terminate
    user input
  • cout ltlt "Enter numbers or end of file to
    quit\n"
  • cout ltlt "ctrl-D" ltlt endl
  • double x
  • int n 0
  • while(cin gtgt x)
  • // process x
  • n

9
eof.cpp
  • // pre input.dat is in the current directory
  • ifstream inFile("input.dat")
  • string s
  • if(!inFile)
  • cout ltlt "Error 'input.dat' not found" ltlt
    endl
  • else
  • cout ltlt "Before, eof " ltlt inFile.eof() ltlt
    endl
  • while(inFile gtgt s)
  • cout ltlt "s " ltlt s ltlt endl
  • cout ltlt " After, eof " ltlt inFile.eof() ltlt
    endl

10
Processing until End of File
  • We often need to extract data from files stored
    on a computer disk.
  • We use ifstream objects for this input.
  • The next slide shows a program that averages
    numbers from the file input.dat that has

70.0 80.0 90.0 75.0 85.0
11
Read until end of file
  • include ltfstreamgt // for class ifstream
  • include ltiostreamgt
  • using namespace std
  • int main()
  • ifstream inFile("input.dat")
  • double number, sum 0.0
  • int n 0
  • while(inFile gtgt number)
  • sum number
  • n
  • cout ltlt "Average " ltlt (sum / n) ltlt endl
  • return 0
  • Output
  • Average 70

12
Event Loop with More Complex Disk File Input
  • Assume this file data represents four employees
    each line represents one employee
  • 40 8.88 1 S Demlow Mary
  • 42 7.77 2 M Barrister Harvey
  • 0 10.00 3 M Manuala Ho
  • 38 9.99 0 S Kline Sue
  • Each line of data could be processed until end of
    file.
  • Code should work with files of different sizes
    (different numbers of employees).

13
Algorithm to process any number of lines
  • For each employee in the file do the following
  • Input all data from one line in the file. This
    data represents one employee.
  • Use data to construct one weeklyEmp object.
  • Use two weeklyEmp member functions grossPay()
    and name()

14
Example Code for eof loop
  • cout.fill('') // set as leading character
  • while(inFile gtgt hours gtgt rate gtgt exemptions
  • gtgt filingStatus gtgt lname gtgt fname )
  • emp weeklyEmp(lname", "fname, hours, rate,
  • exemptions, filingStatus)
  • cout.width(9)
  • cout ltlt emp.grossPay()ltlt" "ltlt emp.name() ltlt
    endl
  • Output
  • 355.20 DEMLOW, MARY
  • 334.11 BARRISTER, HARVEY
  • 0.00 MANUALA, HO
  • 379.62 KLINE, SUE

15
Mixing Numbers and Strings
  • Use care when input has a mix of numeric and
    alphanumeric data.
  • 40 8.88 1 S Demlow Mary
  • 42 7.77 M 2 Barrister Harvey
  • Match input order to file data or vice versa
  • It is all too easy to

Unexpected character destroys the input stream
16
getline
  • Use getline to read in lines of data as one
    string. Here is the function heading
  • istream getline(istream is, string s,
  • char sentinel '\n')
  • Examples
  • string name, address
  • getline(cin, address) // 1313 MockingB Lane
  • getline(inFile, name, '.') // Fatts, Ernest T.
  • while( getline(infile, address) )
  • // process address

17
ofstream objects
  • The files storing large amounts of data are
    typically created by programs that send output to
    those files rather than the screen
  • The ofstream class is used to represent a disk
    file for output
  • General form
  • ofstream object-name ( "file-name" )

18
Sample Program
  • include ltfstreamgt // for class ifstream
  • include ltiostreamgt
  • using namespace std
  • include "myfuns" // for decimals
  • int main()
  • ofstream fout("output.dat")
  • fout ltlt "This does not go to the screen" ltlt
    endl
  • cout ltlt "This does" ltlt endl
  • double x 1.23456789
  • fout ltlt x ltlt endl
  • decimals(fout, 3)
  • fout ltlt x ltlt endl
  • decimals(fout, 6)
  • fout ltlt x ltlt endl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com