C Tutorial

Back to Tutorials

Next Lesson


Lesson 8: Basic File I/O

Lesson Requirements
A C compiler.

Instructions on using the LCCWin32 Compiler.

Lesson Summary
A quick and simple introduction to file I/O. Today you`ll learn how to read and write text files.


Reading A File:
Below is some simple code to read 10 lines from a text file and display them on the screen.
NOTE: you may not know the length of the file, dealing with that is covered later.

void ReadText(char filename[13])
	FILE *inFile; 
	char in_Line[255];
	int i;

	inFile = fopen(filename, "r"); 

	if (inFile == NULL)

	for (i = 1; i <= 10; i++)
		fscanf(inFile, "%s\n", &in_Line); 
		printf("%s\n", in_Line);

download some example code here.
ok lets go over this then:

FILE *inFile declares a variable of type pointer to FILE. FILE is a type defined in the standard C libraries (stdio.h)
you dont need to worry about it in anymore detail at the moment. The line:

inFile = fopen(filename, "r"); 

sets inFile to point to a FILE structure which was created by the function fopen() fopen is declared as:

FILE *fopen( const char *filename, const char *mode );

that means it is a function which takes a pointer to a string which is the filename to open, and a pointer to a string
which defines how to open the file, the modes are:

mode specifies the type of access requested for the file, as follows:

"r" Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w" Opens an empty file for writing. If the file exists, its contents are lost.
"r+" Opens for both reading and writing. (The file must exist).
"w+" Opens an empty file for both reading and writing. If the given exists, its contents are lost.

NOTE: there are a few others but I suggest you reffer to a good C reference book for them.

Next we have:

if (inFile == NULL)

this verifies we succeeded in opening the file (fopen returns a NULL pointer if it fails), if we couldnt open the file
it exits the program. The next new function is:

fscanf(inFile, "%s\n", &in_Line); 

This works exactly the same as scanf which we saw in tutorial number 3, however we now have to specify the file with
its file pointer as the first parameter.

Finally we have:


to close the file. Always make sure you do close any files you have opened.
Writing to a file:
Basically its more of the same, you use the function: fprintf which behaves as prontf does except it also takes a FILE 
pointer as its first parameter i.e.

	FILE *outFile;

	outFile = fopen("test.out", "w");

	fprintf(outFile, "Write this out\n");


Exercise: write a proper WriteText function.

Coping with unknown file sizes:

The earlier read code is fine if you know how many lines you want to read however this isnt always the case so we can use 
the EOF marker (EOF means Endo Of File):

while(inFile != EOF)
	fscanf(inFile,"%s\n", &inString);

where inFile is a previously opened file.



Back to Tutorials

Next Lesson


Copyright © 1997, John Crickett & Neil Henderson.
Legal Information