Details of file input output using C

C file input output : When we compile and run a program we get different output for different input from it. We can not save our output in this way and if we want to know that what was the output of our program a few days or a few hours ago, then we have no chance to get that.

Consider another fact that we have to enter a large amount of data as input when we run our program. But typing many input to the console is not a good practice because we have no chance to go back when we give input from our console.

C file serves the purpose to handle these difficulties. If we write our output to the file then we can easily see the output after a few days. Again if we want to take input from a file then it has no chance to become mistake as we can edit our file easily.

In this guide we will learn about file input output (I/O) in C and see some commonly used operation with file input and output in C. So, let’s get started.

Before getting started of this file input output guide let’s see a C program to understand how we can work with file in C. Don’t worry if if can not understand the program. We will discuss everything bellow in this file input output guide that how the program works.

Example C program with file input output

// program for file input output in C
#include <stdio.h>
int main(){
  FILE *file_pointer;    // file pointer

  char ch;

  file_pointer = fopen ("example file.txt", "r");    // file open in read mode

  while(1){
    ch = fgetc(file_pointer);
    if(ch == EOF){
      break;
    }else{
      printf("%c", ch);
    }
  }

  fclose(file_pointer);
  return 0;
}

You will not see any output when you compile and run this program. Because we have opened the file example file.txt here. At first you have to create a file with this name at the same directory where you have saved this C program file.

Now, lets create a file using this name to the same directory and write something in it then run the program again.

example file.txt

I am learning C programming with competitiveprogrammer.com and it is the right place to learn C programming. Here, I am learning file input output using C.

Now run the program. You will see the following output.

file-io-in-c-file-input-and-output-in-c-c-file-handling

Working with C files

We can create, update, take input, open or close a file using C program. See some commonly used operation which we will cover in this file input output article.

  • Create a file
  • Open a file
  • Update a file
  • Read data from file
  • Delete file

How to create and open a file

If we write the following code then this will create a file and open this. Remember that this file will be created in the same directory where this C file is located. However you can change the directory using file path.

FILE *ptr;
  ptr = fopen("example file.txt", "w");

This code will open the file example file.txt in write mode. This will create the file if the file does not exist in this directory.

Opening file in different mode

Second parameter of the above example is “w” which indicates that we have opened the file in writing mode. We can open a file in different mode. See the file opening mode bellow;

For text file
  • w — We can only write in the file by opening it in “w” mode.
  • r — Opening the file in reading mode. We can not write anything.
  • a — Opening the file in append mode
  • w+ — Opening the file in both read and write mode
  • r+ — Opening the file in both read and write mode
  • a+ — Opening the file in append mode for both reading  and writing
For binary file
  • wb — Opening a binary file in writing mode
  • rb — Opening the binary file in reading mode
  • ab — Opening binary file in append mode
  • wb+ — Opening binary file in both read and write mode
  • rb+ — Opening binary file in both read and write mode
  • ab+ — Opening the binary file in append mode for both reading  and writing

Text file and binary file : Text files are normal files which contains plain text where binary file are file contains binary data. These are mostly files with .bin extensions. All the data here are stored in binary format which is not human readable.

Updating a file using C language

We can rewrite the content of our file by C for updating the file. See the following program to update a file using C program.

// updating a file using c
#include <stdio.h>
int main(){
   char c;
   FILE *f_ptr;
   f_ptr = fopen("example file.txt", "w");

   if(f_ptr == NULL){
      printf("File does not exist in this directory.\n");
      exit(1);
   }

   printf("Enter any character to write on the file : ");
   scanf("%c",&c);

   fprintf(f_ptr,"%c", c);
   fclose(f_ptr);    // closing the file

   return 0;
}

This program will write the character you enter every time. Notice that it will write from beginning every time you run the program.

We will see more C program to update a file in our next guide C file handling. Now, let’s go ahead of this file input output guide.

Method of reading data from C file

We can read data or taking input from file using several built in function in C. We will use fgetc() function here to read data from a file. fgetc() reads all the data from file moving the pointer one by one and return EOF at the end of the file. For this we can use this EOF to determine the end of a file. Now, let’s see the program to read data from a file using fgetc() function.

Before run the program let’s update our file and write something to it.

example file.txt

I am learning C language from competitiveprogrammer.com and here I am for learning c file input output..

// read data from a file using fgetc() function
#include <stdio.h>
int main(){
  FILE *f_pt;

  char ch;

  f_pt = fopen ("example file.txt", "r");

  while(1){
  ch = fgetc(f_pt);
    if(ch == EOF){
      break;
    }else{
      printf("%c", ch);
    }
  }
  fclose(f_pt);
  return 0;
}

Compile and run the program and you will see the output like this :

c-file-io-file-input-output-in-c-read-data-from-file

Deleting a file in C language

We can use remove() function to delete a file. This function takes the file name with its extension as the only parameter.

// C function for deleting a file

remove("example file.txt");
// c program to delete a file
#include<stdio.h>

int main(){
   if (remove("example file.txt") == 0){
      printf("File is successfully deleted.\n");
   }else{
      printf("File can not be deleted.\n");
   }
   return 0;
}

Run this program two times to understand about remove() function. First time it will find the file and delete this. But second time, as our file does not exist there, so it will give different output.

Output for first time run the program

File is successfully deleted.
Output for second time run the program

File can not be deleted.

We can do more without file input output using C language. We have discussed more about file handling in our next guide. So, see our next guide to get more knowledge about file handling in C.

Previous pageDynamic memory allocation

Next pageFile handling in C