C Text Files chp 7 readwrite - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

C Text Files chp 7 readwrite

Description:

A File is an external collection of related data treated as a unit. ... (notepad, borland, word,etc.) Binary files. 1. Are stored as integer and floating point ... – PowerPoint PPT presentation

Number of Views:65
Avg rating:3.0/5.0
Slides: 24
Provided by: salga5
Category:
Tags: borland | chp | files | readwrite | text

less

Transcript and Presenter's Notes

Title: C Text Files chp 7 readwrite


1
C Text Files chp 7read/write
CIS-161 Prof S.F. Gambino
2
Text Files I/O chp 7
  • Files
  • A File is an external collection of related data
    treated as a unit.
  • The primary purpose is to keep a record of data
    permanently
  • Saved usually on secondary storage devices like
    Disk, CDs, etc.
  • Type of Files Text files and Binary files
  • Text files 1. Are stored as characters
  • 2. Organized around lines and each line ends
    with \n character
  • 3. can be created, updated and opened with a
    text editor such as (notepad, borland,
    word,etc.)
  • Binary files
  • 1. Are stored as integer and floating point
  • 2. Cannot be opened by a text editor. The data
    is unreadable
  • 3. Chapter 14 is dedicated to binary files

3
Text Files I/O chp 7
Problem Given the length and the width of a
rectangle. Compute the area of the rectangle.
Use a text file to store the length and the width
of three or more rectangles. Also note that the
values are integer type. The program should have
the following functions 1. main 2.
getData 3. calcData 4.
writeData Input.. length,
width Output... area Process arealength
width
4
Data length, width
External File name
5
Relationship between streams, internal and
external names
Internal nameMemory or where the program
residesduring execution
A file stream is needed to use the text file. A
file stream needs to be created, connected and
disconnected.
6
Create a stream name
Internal namefsinputWhen you are writing a
program your starting point IS FROM MEMORY
ifstream
Create the ifstream ifstream fsinput
7
Connect the stream name to the file
Internal namefsinputWhen you are writing a
program your starting point IS FROM MEMORY
ifstream
Connecting the ifstream to textdata.dat
fsinput.open ( textdata.dat)
8
Open possible error
Connecting the ifstream to textdata.dat
fsinput.open ( textdata.dat) if (
!fsinput ) cerr ltlt \n Error 100 open failed
\n exit(100)
.. Continue with program statements
etc
9
Open possible error SPELLING
Connecting the ifstream to textdata.dat
fsinput.open ( txtdta.dat) if ( !fsinput
) cerr ltlt \n Error 100 open failed \n
exit(100)
.. Continue with program statements etc
10
Get or read the text file
Internal namefsinputMEMORY
ifstream
Get or read the data from textdata.dat while (
getData( fsInput, length ,width) )
Compute(. Display( .
Data length, width
11
Data length, width
Get or read the text file
int getData(ifstream fsInput, int length, int
width) fsInput gtgt length gtgt width if (
!fsInput ) if ( fsInput.eof() ) return
0 else cerr ltlt "\n read error...
" getch() return 0 return
1
cin gtgt length gtgt width
12
Disconnect the stream name from the file
Internal namefsinputMEMORY
ifstream
Disconnecting the ifstream from textdata.dat
fsinput.close()
13
Disconnecting possible error
Disconnecting the ifstream from textdata.dat
fsinput.close() if ( fsinput.fail()
) cerr ltlt \n Error 102 , close failed
\n exit(102)
14
program
include ltiostream.hgtinclude ltfstream.hgt includ
e ltstdlib.hgt void main() int length, width,
area ifstream fsinput fsinput.open (
textdata.dat) if ( !fsinput ) cerr ltlt
\n Error 100 open failed \n
exit(100) fsinput gtgt
length gtgt width area length width
cout ltlt \n len is ltlt length cout
ltlt \n wid is ltlt width cout ltlt \n area
is ltlt area fsinput.close() if (
fsinput.fail() ) cerr ltlt \n Error 102 ,
close failed \n exit(102)

This will only get the first set of data
15
program
include ltiostream.hgtinclude ltfstream.hgt includ
e ltstdlib.hgt void main() int length, width,
area ifstream fsinput fsinput.open (
textdata.dat) if ( !fsinput ) cerr ltlt
\n Error 100 open failed \n
exit(100)
while( getData ( fsinput ,
length , width ) ) area length
width cout ltlt \n len is ltlt
length cout ltlt \n wid is ltlt width
cout ltlt \n area is ltlt area cout ltlt "\n
---------------------------"
fsinput.close() if ( fsinput.fail() )
cerr ltlt \n Error 102 , close failed \n
exit(102)

By adding a loop we can get the entire file
16
Create a stream nameto write to a text file
Internal namefsoutMEMORY
ofstream
Create the ofstream ofstream fsout
17
Connect the stream name to the file
Internal nameMEMORY
ofstream
Create Connect the ofstream to io6data.dat
fsout.open ( io6data.dat)
18
Open possible error
Connecting the ofstream to io6data.dat
fsout.open ( io6data.dat) if ( !fsout
) cerr ltlt \n Error 102 open failed \n
getch( ) exit(102)
.. Continue with program
statements etc
19
Write to the text file
Internal namefsout MEMORY
ofstream
Note fsout is very similar to the usual
cout. Use fsout to write to the text file that we
are connected
20
Write to the text file
  • Use fsout or the internal name to write to a text
    file
  • Always use formatted output. ( iomanip.h)
  • Use a function to complete the task

Department ..string (15) Number of
employees. integer Total sales .float
Possible function prototype for writing the
above data to disk void writeData ( ofstream
fsout, char dept , int num, float totsales )
21
Department ..string (15) Number of
employees. integer Total sales .float
Function prototype void writeData ( ofstream
fsout, char dept , int num, float totsales
) Function Call writeData( fsout, dept, num,
totsales ) Function header details void
writeData ( ofstream fsout, char dept , int
num, float totsales ) fsout ltlt
setiosflag(iosleft) ltlt endl fsout ltlt
setw(20) ltlt dept fsout ltlt setw(7) ltlt num
fsout ltlt setiosflag(iosright) fsout ltlt
setw(10) ltlt totsales fsout ltlt
\n---------------------------------
cout ltlt setiosflag(iosleft) ltlt
endl cout ltlt setw(20) ltlt dept cout ltlt setw(7)
ltlt num cout ltlt setiosflag(iosright) cout ltlt
setw(10) ltlt totsales cout ltlt \n-----------------
--
22
Disconnect the stream name from the file
Internal namefsout MEMORY
ofstream
Disconnecting the ofstream from io6data.dat
fsout.close()
23
Disconnecting possible error
Disconnecting the ofstream from io6data.dat
fsout.close() if ( fsout.fail() )
cerr ltlt \n Error 102 , close failed \n
exit(102)
Write a Comment
User Comments (0)
About PowerShow.com