Title: Simple File IO
1Simple File I/O
Week 7
- Component objectives
- Create a Simple text File.
- Open and Close Files from a program.
- Read,Write and Manipulate Data held in a text
file.
2Introduction
- File can be
- simply a text file created in Notepad
- or else they can be generated by C/C programs.
- referred to as External or Physical Files.
- When referring to Files the two operations
associated are - Outputting to a File (Writing or creating the
file) - Inputting from a file (Getting the contents of
the file)
3Outputting to a File
- The following steps are necessary to successfully
output to a file. - The file must be declared.
- The file must be opened
- The file can be written to.
- The file must always be closed.
- If a file exists with the same name that you are
outputting to then it will be overwritten.. so
beware.
4An Example of Outputting
- ofstream myoutfile //Declare
- myoutfile.open(test.dat) // Open
- myoutfile ltlt iAge //Write
- myoutfile.close() //Close
5A Full program for Outputting
- void main(void)
- ofstream outfile // Declare File
- char Filename test.dat //Variable
(Filename) to hold name of external File - char cContinue y
- char Name21 // Variable to hold an inputted
name - int iAge // Variable to hold an inputted age
- outfile.open(Filename) // Open File
- if (outfile.fail()) // Error checking routine
in case file cannot be opened. - cout ltlt Failed to open output file
- // End of error checking
- else
- while(cContinuey) // Loop while
user enters a y to continue - strcpy(Name,
) //clears the string - cout ltlt Please enter name
// prompt user to enter a name - cin.get(Name,21,\n) // Store
name in Variable Name - cin.ignore(20,\n)
- cout ltlt and an age please
// prompt user to enter an age - cin gtgt iAge // store age
entered in Variable iAge
void WriteString(ofstream outfile,char
sString,int iCount) for(int iLoop
0iLoopltiCountiLoop)
outfile.put(sStringiLoop)
char Again(void) char cReply int
valid0 while (!valid) cout ltlt Do you
wish to continue (y/n)? cin gtgt
cReply cin.ignore(1,/n) valid
(cReply y) (cReply n) //end
while //end of function
Rick Shaw 37 Sandy Shore 60 Morris
Minor 45
Contents of test.dat
6Input Files
- The following steps are necessary to successfully
read from a file. - The file must be declared.
- The file must be opened
- The file can be read.
- The file must always be closed.
- If the file you are trying to open does not exist
then you will get an error. - Use eof function from ltfstream.hgt to check for
en of file.
7Declaring an Input File
- ifstream myinfile //Declare
- myinfile.open(test.dat) // Open
- myinfile gtgt iAge //Read
- myinfile.close() //Close
8A Sample program for Reading
- void main(void)
- ifstream infile // Declare File
- char Filename test.dat //Variable to
hold name of external File - char Name21 // Variable to hold a read name
- int iAge // Variable to hold a read age
- infile.open(Filename) // Open File
- if (infile.fail()) // Error checking routine
in case file cannot be opened. - cout ltlt Failed to open input file
- // End of error checking
- else
- while(!(infile.eof())) // Loop until
end of file is reached - ReadString(infile,Name,21) //
read in 21 characters - infile gtgt iAge // read in
numeric value for age - ReadEoln(infile) // move to
next line - cout ltlt Details are ltlt Name
ltlt ltlt iAge ltlt endl //display details - // end of while loop
- infile.close() //close opened file
- // end of if
Void ReadEoln(ifstream infile) infile.ignore(70
,/n) //end of function Function to cope
with reading EndofLine marker
9Summary
- This lecture has covered
- User-definable data types
- How to code simple structures.
- How to code nested structures.
10Appendix Slide for 2 Examples
This code needs to be added to the 2 example
programs in order that you can compile them (both
programs courtesy Helen Ashdown 1994)
- include ltiostream.hgt //Writing Data to A File
- include ltfstream.hgt
- include ltfstring.hgt
- include ltconio.hgt
- include ltiomanip.hgt
- void WriteString(ofstream outfile,char
sString,int iLen) - char Again(void)
include ltiostream.hgt // Reading Data from a
File include ltfstream.hgt include
ltfstring.hgt include ltconio.hgt void
ReadString(ifstream infile,char sString,int
iLen) void ReadEoln(ifstream infile)