Skip to main content

Reading and Writing Files in C

Introduction

In C programming, file handling plays a significant role. It allows us to store data in files for later retrieval, making it easier to manage large amounts of data. This tutorial will walk you through the basics of reading from and writing to files in C. We will cover the key functions, like fopen, fclose, fgetc, fputc, fprintf and fscanf.

File Operations in C

Before we can read or write a file, we need to open it using the fopen function. It has the following syntax:

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

Here, filename is the name of the file to be opened and mode defines the purpose of opening the file. Some of the most commonly used modes include:

  • r: open the file for reading.
  • w: open the file for writing (creates a new file if it doesn't exist, or clears the file's contents if it does).
  • a: open the file for appending (writes data at the end of the file without deleting the existing content).

After performing the necessary operations, we should close the file using the fclose function. It helps to free up the buffer, or temporary storage area. The syntax is:

int fclose( FILE *fp );

Here, fp is the file pointer which you want to close.

Reading from a file

To read from a file, we can use the fgetc function. It reads a character from a file and returns it. The syntax is:

int fgetc( FILE *fp );

Consider the following example:

FILE *fp;
char ch;

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

if (fp == NULL)
{
printf("File doesn't exist \n");
return;
}

while ((ch=fgetc(fp)) != EOF) // read the file until end
{
printf("%c", ch); // print the content of file
}

fclose(fp); // close the file

Writing to a file

To write to a file, we can use the fputc function. It writes a character to a file and the syntax is:

int fputc( int c, FILE *fp );

Consider the following example:

FILE *fp;
char ch = 'A';

fp = fopen("file.txt", "w"); // open file in write mode

if (fp == NULL)
{
printf("File doesn't exist \n");
return;
}

fputc(ch, fp); // write the character to file

fclose(fp); // close the file

Reading and Writing Strings

For reading and writing strings, we can use fscanf and fprintf functions respectively.

The fscanf function reads formatted input from a file. The syntax is:

int fscanf( FILE *fp, const char *format, ... );

The fprintf function writes formatted output to a file. The syntax is:

int fprintf( FILE *fp, const char *format, ... );

Here is an example:

FILE *fp;
char buff[255];

fp = fopen("file.txt", "w+");
fprintf(fp, "This is testing for fprintf...\n");
fclose(fp);

fp = fopen("file.txt", "r");
fscanf(fp, "%s", buff);
printf("1 : %s\n", buff );
fclose(fp);

Conclusion

In this tutorial, we learned about the basics of file handling in C. We learned how to open, close, read from, and write to files. We also learned how to handle strings while reading from and writing to files. Remember to always close the file after performing the necessary operations to free up the buffer. Happy Coding!