Download Files in C - Intro to Computer Programming - Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!
File I/O in C
Lecture 7
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.
Opening Files
fptr1 = fopen ( "mydata", "r" ) ; would open the file mydata for input (reading).
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.)
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 mydata does not exist. Then: FILE *fptr1 ; fptr1 = fopen ( "mydata", "r") ; if (fptr1 == NULL) { printf ("File 'mydata' did not open.\n") ; }
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 feof function which returns a true or false condition: fscanf (fptr1, "%d", &var) ; if ( feof (fptr1) ) { printf ("End-of-file encountered.\n”); }
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 fscanf function:
int istatus ;
istatus = fscanf (fptr1, "%d", &var) ;
if ( istatus == EOF )
printf ("End-of-file encountered.\n”) ;
Closing Files
fclose ( fptr1 ) ;
fclose ( fptr2 ) ;
will close the files and release the file
descriptor space and I/O buffer memory.
Reading and Writing Files
#include <stdio.h>
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) ; Docsity.com
A Little Help for Assignment G
- Some things that make it easier to understand:
- Copy the file from the common directory to your directory
- Use more g06.dat to 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 dat
- You want to sum these columns so you need a couple more variables, example: sum1 and sum
A Little Help for Assignment G
- 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?)
A Little Help for Assignment G
- In order to use the for loop, the counter, k,
must be declared to be an integer at the
beginning of the main function along with all
of the other variables