Lecture 07 - File IO in C - 06
-
Upload
midhungbabu88 -
Category
Documents
-
view
215 -
download
0
Transcript of Lecture 07 - File IO in C - 06
-
8/9/2019 Lecture 07 - File IO in C - 06
1/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 1Winter Quarter
File I/O in C
Lecture 7
-
8/9/2019 Lecture 07 - File IO in C - 06
2/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 2Winter Quarter
Files in C
In C, each file is simply a sequential stream of
bytes. C imposes no structure on a file.
A file must first be opened properly before it can
be accessed for reading or writing. When a file is
opened, a stream is associated with the file.
Successfully opening a file returns a pointer to
(i.e., the address of) a file structure, which
contains a file descriptor and a file control block.
-
8/9/2019 Lecture 07 - File IO in C - 06
3/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 3Winter Quarter
Files in C
The statement:
FILE *fptr1, *fptr2 ;
declares that fptr1 and fptr2are pointer variablesof type FILE. They will be assigned the address
of a file descriptor, that is, an area of memory that
will be associated with an input or output stream.
Whenever you are to read from or write to the file,you must first open the file and assign the
address of its file descriptor (or structure) to the
file pointer variable.
-
8/9/2019 Lecture 07 - File IO in C - 06
4/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 4Winter Quarter
Opening Files
The statement:
fptr1 = fopen ( "mydata", "r" ) ;
would open the file mydata for input (reading).
The statement:
fptr2 = fopen ("results", "w" ) ;
would open the file results for output (writing).
Once the files are open, they stay open until you
close them or end the program (which will close
all files.)
-
8/9/2019 Lecture 07 - File IO in C - 06
5/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 5Winter Quarter
Testing for Successful Open
If the file was not able to be opened, then the
value returned by the fopen routine is NULL.
For example, let's assume that the file mydatadoes not exist. Then:
FILE *fptr1 ;
fptr1 = fopen ( "mydata", "r") ;
if (fptr1 == NULL)
{
printf ("File 'mydata' did not open.\n") ;
}
-
8/9/2019 Lecture 07 - File IO in C - 06
6/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 6Winter Quarter
Reading From Files
In the following segment of C language code:
int a, b ;
FILE *fptr1, *fptr2 ;
fptr1 = fopen ( "mydata", "r" ) ;
fscanf ( fptr1, "%d%d", &a, &b) ;
the fscanffunction would read values from thefile "pointed" to by fptr1 and assign those values
to a and b.
-
8/9/2019 Lecture 07 - File IO in C - 06
7/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 7Winter Quarter
End of File The end-of-file indicator informs the program
when there are no more data (no more bytes) to
be processed.
There are a number of ways to test for the end-of-
file condition. One is to use the feoffunction
which returns a true orfalse condition:
fscanf (fptr1, "%d", &var) ;
if ( feof (fptr1) )
{
printf ("End-of-file encountered.\n);
}
-
8/9/2019 Lecture 07 - File IO in C - 06
8/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 8Winter Quarter
End of File
There are a number of ways to test for the end-of-
file condition. Another way is to use the value
returned by the fscanffunction:
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == EOF )
{
printf ("End-of-file encountered.\n) ;
}
-
8/9/2019 Lecture 07 - File IO in C - 06
9/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 9Winter Quarter
Writing To Files
Likewise in a similar way, in the following
segment of C language code:
int a = 5, b = 20 ;FILE *fptr2 ;
fptr2 = fopen ( "results", "w" ) ;
fprintf ( fptr2, "%d %d\n", a, b ) ;
the fprintffunctions would write the values stored
in a and b to the file "pointed" to by fptr2.
-
8/9/2019 Lecture 07 - File IO in C - 06
10/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 10Winter Quarter
Closing Files
The statements:
fclose ( fptr1 ) ;fclose ( fptr2 ) ;
will close the files and release the file descriptor
space and I/O buffer memory.
-
8/9/2019 Lecture 07 - File IO in C - 06
11/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 11Winter Quarter
Reading and Writing Files
#include
int main ( )
{
FILE *outfile, *infile ;
int b = 5, f ;
float a = 13.72, c = 6.68, e, g ;
outfile = fopen ("testdata", "w") ;fprintf (outfile, "%6.2f%2d%5.2f", a, b, c) ;
fclose (outfile) ;
-
8/9/2019 Lecture 07 - File IO in C - 06
12/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 12Winter Quarter
Reading and Writing Files
infile = fopen ("testdata", "r") ;
fscanf (infile,"%f %d %f", &e, &f, &g) ;
printf ("%6.2f%2d%5.2f\n", a, b, c) ;
printf ("%6.2f,%2d,%5.2f\n", e, f, g) ;
}
12345678901234567890
****************************
13.72 5 6.68
13.72, 5, 6.68
-
8/9/2019 Lecture 07 - File IO in C - 06
13/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 13Winter Quarter
A Little Help for Assignment G06
Some things that make it easier to understand:
Copy the file from the common directory to your
directory
Use more g06.datto see what is in the file
What kind of numbers are the numbers in the file?
How many columns and how many rows?
Based on your observations, you can declare a
couple of variables, example: dat1 and dat2
You want to sum these columns so you need a
couple more variables, example: sum1 and sum2
-
8/9/2019 Lecture 07 - File IO in C - 06
14/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 14Winter Quarter
A Little Help for Assignment G06
You want the average of the columns so you
need some additional variables.
You will be dealing with a couple of files so
you need a couple of file pointers
Assignment G06 requires us to read several
lines of some information from a disk file and
write it out to the screen and to a file. (Sounds
repetitive, doesn't it?)
-
8/9/2019 Lecture 07 - File IO in C - 06
15/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 15Winter Quarter
A Little Help for Assignment G06
Simple repetitive tasks are easily performed with
a "for" loop. We'll talk more about forloops very
soon. Today, use something like the following:
for ( k = 1 ; k
-
8/9/2019 Lecture 07 - File IO in C - 06
16/16
Engineering H192 - Computer Programming
The Ohio State University
Gateway Engineering Education Coalition
Lect 7 P. 16Winter Quarter
A Little Help for Assignment G06
In order to use the forloop, the counter, k, must
be declared to be an integer at the beginning of
the main function along with all of the other
variables