File input/output in C

by | Aug 19, 2022 | C

Home » C » File input/output in C

Introduction

This part of programming basically deals with the permanent saving of a file. When a program runs, the data is in the memory, but when it ends, or the computer shuts down, all data gets lost. To store the data permanently, we need to write it to a file.

The file is used to store data. In this article, we will learn how to read data from a file and write data in the file.

Declaration of a file

A file is declared using a pointer of type ‘FILE’.

FILE *f;

f’ is a pointer to a file.

There are some functions we can perform with files. The list of those functions is given below:

Function Description
fopen() Opening a file that already exists or creating a new file is done with this function.
fclose() To close a file.
fgetc() The character in a file can be read using this function.
fputc() To write a character to a file
fscanf() To read a string from a file.
fprintf() To write a string to a file.

Opening of a file

The fopen( ) function is used to open an existing file or to create a new file.

Program

#include<stdio.h>

int main()

{

FILE *f;

f=fopen("fileopen","mode");

return 0;

}

 

In the above code, ‘f’ is a pointer to the file, ‘fileopen’ is the name of the file which we want to open, and ‘mode’ is the reason for opening the file.

The table below shows the various values that a mode can take in a text file.

Mode Description
r This mode opens a text file for reading.
w It opens a text file for writing.
a It opens a text file to add text at the end, i.e., for appending.
r+ Opening of a text file for reading and writing.
w+ It opens a text file for reading and writing. If the file exists, then it first cuts the length to zero; else, create a file if it doesn’t exist.
a+ It opens a text file that is both in reading and writing mode. Reading always starts from the beginning, but writing can only be done at the end.

For a binary file, the various values mode can take are:

rb, wb, ab, rb+, wb+, ab+.

In this article, we will only learn about the text files.

The process of opening a file is as follows,

fopen( ) searches for the file on the disk and then loads the file on the memory called the buffer. The changes are made in the buffer memory before closing the file.

Closing a file

An opened file can be closed by using the fclose( ) function.

Program

#include<stdio.h>

int main()

{

FILE *f;

fclose( f );

return 0;

}

 

If a file is closed successfully, then fclose( ) function will return 0. But if it faces some error while closing the file, EOF (End of file) is returned.

Reading and writing on a file

fgetc( ) and fscanf( ) are the functions used for reading and fputc( ) and fprintf( ) are the functions used for writing in a file. But the use of these functions completely depends on whether we deal with characters or strings.

Functions fgetc( ) and fputc( ) are used for reading and writing characters in a file respectively.

Functions fscanf( ) and fprintf( ) are used for reading and writing strings in a file respectively.

Use of fgetc() and fputc()

fgetc(): It reads a character from a file opened in reading mode using fopen() and returns EOF when it reaches the end of the file.

fputc(): It writes a character to the file which is already opened in write mode using fopen().

Program:

#include<stdio.h>

int main()

{

FILE *f;

char c;

f=fopen("prog.txt","r");

while(c!=EOF)

{

c=fgetc(f);

printf("%c",c);

}

fclose(f);

return 0;

}

 

Output:

f=fopen(“prog.txt”,”r”); this statement opens a file named ‘prog.txt’ in read mode. Read mode allows us to read data from the file, but the file ‘prog.txt’ must exist on your device.

In the while loop, c=fgetc(f); reads the value of ‘c’ from the file ‘prog.txt’ until the entire value of ‘c’ has been read from the file and reaches EOF.

fclose(f); closes the file that we had opened.

Since a variable ‘c’ is used to read the data of the file, we use the fgetc() function.

Using fscanf() and fprintf()

These functions are used for reading and writing strings in a file.

Program:

#include <stdio.h>

int main()

{

FILE *fr;

char s[40];

int n;

fr = fopen("welcome.txt", "w");

printf("Enter a string and a number");

scanf("%s%d", s, &n); /*read from keyboard*/

fprintf(fr, "%s %d", s, n); /*  write to file*/

fclose(fr);

fr = fopen("welcome.txt", "r");

fscanf(fr, "%s%d", s, &n); /*read from file*/

printf("%s %d", s, n); /*display on screen*/

fclose(fr);

return 0;

}

 

Output:

Firstly, we created a file named ‘welcome.txt’. Using the fopen() function, we open it in write mode. Then, we enter a string value and an integer value through the keyboard using the scanf() function. The entered values get stored in variables ‘s’ and ‘t’, respectively.

fprintf writes the string and the integer value to the file, and fclose(f) closes the file.

Now, in the end, we will read the file’s contents and then print those on the screen.

fopen function opens the file in reading mode. The fscanf function reads the file’s data, and the printf function prints the data on the output screen.

We can also copy a file into the other file. This is additional work. Create a file named ‘source.txt’ on your device or replace the file name in the code with the file name that exists on your device.

#include <stdio.h>

int main()

{

FILE *s,*t;

char c;

s = fopen("source.txt", "r");

t = fopen("target.txt", "w");

while(1){

c = fgetc(s);

if(c == EOF)

{

break;

}

else

{

fputc(c,t);

}

}

 

fputc(c,t) will copy the character stored in ‘c’ to the file opened as ‘t’.

 

Author

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author