File Handling in C – Part 3

Read: File Handling in C – Part 2

In the last tutorial I told you about the program to calculate the number of spaces, blanks, tabs and characters. You might have noticed that I have used the similar logic of file reading program in that tutorial. Today I will continue the quest of learning file input output function by showing you a program to copy contents from one file to another.

File Handling in C

C program to copy contents of one file to another file

Before writing the program I would like to introduce you to one new function fputc(). It’s a counter part of fgetc() function. As its name suggests this function is used to write some content in the file. Consider the below program.

This program will copy contents of demo.txt file to new.txt.

1. In this program I have created two file structure pointer named as fsource and ftarget. As its name suggests they are used for source and target files. I have also declared one character variable ch. 

2. After that I have checked whether the source file is successfully opened for reading or not. I have used two if statements for that. This can be checked by putting the condition (file_pointer_name==NULL). The same checking can be done for second file which we are using for writing purpose.

3. Now I have started while loop and inside it fgetc() function is used to fetch one character from the source file and the writing that character to target file using fputc() function.

4. The while loop will continue till the file pointer fsource reaches to end of file demo.txt. The end is encountered when value of ch become EOF.

5. In the last I have closed both the files using fclose() functions.

Note: Generally we use buffer to write some content into some file because it will be inefficient to access disk repeatedly to write just one character. Functionality of buffer will be similar to the file reading program.

File Opening Modes in C

In the above example I have opened the source file with reading mode and target file with writing mode. However there are some more modes that are available in C. Lets study their use too.

“r” – It is used for reading from file. It searches for the file on the disk.  In this mode fopen() returns NULL if it can’t open the file otherwise it setup the pointer to point the first character of the file.

“w” – It is used for writing into file. It searches the file inside the disk, if it is absent then it will create one on the disk.

“a” – It does similar task like “w”. Used for appending, that means it points the pointer at the end of the file for writing.

“r+” – Open file for both reading and writing. The file must already exist.

“w+” – Open file for both reading and writing. Creates a new file if file doesn’t exist.

“a+” – Open file for reading and appending purpose.

Note: In all the above modes if fopen() function is unable to open the file due to some reason then it will return NULL.

1 thought on “File Handling in C – Part 3”

Leave a Comment

Your email address will not be published. Required fields are marked *