Reading and Writing Operation on File System in C Programming

Introduction

The standard input devices and the standard output devices are treated as a file in the UNIX system. We can read the data from the standard input devices using the getchar() and scanf() functions, and how we can write the information on the standard output devices using putchar() and printf() functions. In a similar manner, we would be writing data into files and reading information from files using in-built functions available in C.

Unformatted input and output and formatted input and output

Unformatted input and output were possible using the getchar() and putchar() functions, whereas formatted input and output were possible using the scanf() and printf() functions, where we had provided the format specifiers in the argument list of the functions. Similar to this concept, we have Unformatted input and output operations on a file and formatted input and output operations on a file.

Unformatted input and output operations in files are.

  1. Writing characters to a file
  2. Reading characters from a file

Writing characters to a file

To write data into a file character by character, we make use of the putc() standard function in 'C'. putc() function is analogous to the putchar() function.

Syntax of putc()

putc(<character variable>,<file pointer>);

"character variable " contains the character that one wants to write to the file, which has already been opened in a mode that supports writing to files, i.e. write mode, append mode, write-read mode, or read-write mode. The file has been assigned to the file pointer to perform all the operations on it.

For Example

Write a source code to create a file containing text using putc() function by writing the text character by character.

#include<stdio.h>
#include<conio.h>

void main() {
    FILE *file_pointer;
    char c;

    clrscr();
    printf("Shikha Tiwari: Write a program to create a file containing text using putc() function\n\n");

    if ((file_pointer = fopen("text.dat", "w")) == NULL) {
        printf("Cannot open text.dat file for writing text...\n\n");
        exit(1);
    }

    while ((c = getchar()) != EOF) {
        putc(c, file_pointer);
    }

    fclose(file_pointer);
    printf("Writing of text in text.dat completed...\n\n");

    getch();
}

Output

Output

Open the Text file text.dat, the same output will be stored in the same file.

Text file

Another function which can be used to write data into a file character is the fputc() function. fputc() and putc() function both work in exactly the same manner.

Syntax of fputc()

fputc(<character variable>,<file pointer>);

Reading characters from a file

To read a file character by character, we make use of the getc() standard function that is available to us. getc() function is analogous to the getchar() function.

Syntax of getc()

<character variable>=getc(<file pointer>);

" Character valuable " is one in which we want to read the character from the file that has already been opened in a mode that supports the reading operation, i.e. Read mode, read-write mode, and Write-read mode. The file is handled using a the file pointer, which is already defined and assigned the internal file number of the file that is being read.

For Example

Write a program to read a file containing text character by character using getc() function.

#include<stdio.h>
#include<conio.h>

void main() {
    FILE *file_pointer;
    char c;

    clrscr();
    printf("Shikha Tiwari: Write a program to read a file containing text using getc() function\n\n");

    if ((file_pointer = fopen("text.dat", "r")) == NULL) {
        printf("Cannot open text.dat file for reading text...\n\n");
        exit(1);
    }

    while ((c = getc(file_pointer)) != EOF) {
        putchar(c);
    }

    fclose(file_pointer);
    printf("Reading of text in text.dat completed...\n\n");

    getch();
}

Output

Program

Code Explanation

In the above program, the file is opened in the read mode for reading the data. We have defined a loop for the getc() function to read the file text.dat identified by the file pointer file_pointer, tii the end file EOF is reached. The read character is then displayed on the screen using the putchar() function.

The end of the file marker in the file is normally the CONTROL+D keys combination '^D'. It may be a variant, depending upon the operating system being used, for example, CONTROL+D for DOS.

'C' also provides another function fgetc() to read a file character by character.

Syntax of fgetc()

<character variable>=fgetc(<file pointer>);

End of file

When a file is read, the file pointer moves forward in the file by the number of characters that were read. In the example above, every time the while loop got executed, the file pointer would move forward by one character since we were reading the file character by character.

Similarly, when a file is written the file pointer moves forward by the number of characters that were input. When the writing operation on a file is complete, EOF is put at the end of the file. Any function which enables the reading of data from the file will return EOF when the end of the file has been reached. This implies that the reading of data from the file should terminate when EOF if encountered. Beside the end of the file marker, EOF. 'C' also provides the feof()function, which returns the value true (non-zero) if the end of the file has been reached or returns false (zero).

syntax of feof()

feof(<file pointer>);

"file pointer" contains the internal file name of the file in question.

For Example

Write a program to read a file containing text character by character using getc() function and use feof(0 function to determine if the end of the file is encountered.

#include<stdio.h>
#include<conio.h>

void main() {
    FILE *file_pointer;
    char c;

    clrscr();
    printf("Shikha Tiwari: Write a program to read a file containing text using getc() function\n\n");

    if ((file_pointer = fopen("text.dat", "r")) == NULL) {
        printf("Cannot open text.dat file for reading text...\n\n");
        exit(1);
    }

    while (!feof(file_pointer)) {
        c = fgetc(file_pointer);
        putchar(c);
    }

    fclose(file_pointer);
    printf("Reading of text in text.dat completed...\n\n");

    getch();
}

Output

Function

Code Explanation

The above program is similar to the previous program, which reads the file character by character. The loop while(!feof(file_pointer)) will execute till the end of the file is not reached.

As soon as the end of the file is met, the while loop will terminate.

Summary

In C programming, reading and writing operations in a file system are fundamental tasks used to handle data persistence. The standard I/O library (stdio.h) provides functions like fopen(), fclose(), fwrite(), and fread() to perform file handling. Files are opened using the fopen() function in different modes such as read (r), write (w), append (a), and their binary counterparts (rb, wb, ab). Once a file is open, fwrite () is used to write data to a file, while fread() reads the contents. Proper handling with fclose() ensures that files are securely closed after operations, preventing data corruption. Additional functions like fprintf() and fscanf() allow formatted reading and writing to files.


Similar Articles