top of page
Manipulating Files
Opening Files
To open a file in C we will use the FILE datatype with the fopen() function. To open the file we must first create a variable of type FILE*. With that value we can use fopen() along with the mode in which we will handle the data. In the example below I open a file held in the same directory as the program under the read option.
Example:
Output:


If the file exists in the directory it will open properly and present us with the SUCCESS prompt. If the file is nonexistent we will see ERROR.
Other Modes
We may also open files using other parameters such as w, r+,a. These other modes will allow us to perform different tasks.

Reading Files
After opening a file in read or read/write mode we can now use various functions to read from the file.
getc()
The getc() function allows us to get a single character input of data. After reading a letter, using the function again will serve the next letter in the file. Once the end of the file is reached it will provide data which is not useful. To prevent this we will check if the file has reached its end before looking for input.
Example:
Output: Input:


At the end of the file the loop breaks and the program ends. But what if we want to go through the file multiple times?
rewind()
Using rewind(filePointer); will bring the file position indicator back to the start of the file. This will let us reread the file in order from the beginning once again. Let's make a loop that will utilize this to output our file 10 times.
Example:
Output: Input:


fseek()
The fseek() function can be used to precisely move the file position indicator using an offset and a starting position.
The syntax for fseek is fseek(filePointer, offset, position);
The starting position can be set as SEEK_SET, SEEK_CUR, or SEEK_END. SEEK_SET will set the starting position at the beginning of the file and offset from there while SEEK_CUR will use the current indicator position and SEEK_END will use the end of the file.
Say we want to use fseek to skip the first 14 characters of the file starting from the beginning, we can simply use fseek(fp,14,SEEK_SET);
Example:
Output: Input:


To do the same thing with SEEK_END we use fseek(fp,-9,SEEK_END);
fgets()
While fgetc() gets character inputs fgets()gets string inputs (characters delimited by a new line). Once the end of the line is reached, another gets() must be used to get data from the next line. fgets will also add a termination character to the end of the characters so we can simply output them as a string with printf.
The syntax for fgets is fgets(charArr, size, filePointer);
Example:
Output: Input:


fscanf()
fscanf will work like scanf but take input from a file. Similar to scanf we will need to supply a conversion character.
The syntax for fscanf is fscanf(filePointer, "converisonChar", outputVar);
Example:
Output: Input:


Notice how by default a space delimiter is used and the file position indicator will be left at the start of the next word upon finishing.
Writing To Files
While reading from a file we used fgetc, fgets, and fscanf. While writing to a file we will use their counterparts fputc, fputs, and fprintf. Note that while a file is opened using write it will overwrite the contents and when a file is opened with append it will instead add on to the end of the file.
fputc()
fputc uses the syntax fputc(input, filePointer);
Example:
Output:


fputs()
fputs uses the syntax fputs(input, filePointer);
Example:
Output:


fprintf()
fprintf uses the syntax fprintf(filePointer, input, anyVars);
Example:
Output:


Closing Files
While working with files in our programs they must be closed correctly. Whenever we are done using a file we will close it using fclose(filePointer);
Example:


Output: Input:
bottom of page