Writing to a file means storing data from a C program into a file so that the information can be kept for later use. C provides functions such as fputc(), fputs(), and fprintf() for writing different types of data to a file.
Writing to a file means storing data from a C
program into a file so that the information can be kept for later
use. C provides functions such as fputc(),
fputs(), and fprintf() for writing
different types of data to a file.
The file is opened in write mode so that data can be stored in it.
Once a file has been opened successfully, writing operations are performed using the file pointer rather than the filename. The file pointer identifies the file on which the operation is being performed.
Here, fp refers to the file and
fprintf() writes the message into it.
The fputc() function writes a single character to a file. It is similar to a character-output function, but instead of displaying the character on the screen, it writes the character to the file represented by the file pointer.
The character A is written to the file referred
to by fp.
The fputs() function writes a string to a file. It is useful when an entire string needs to be stored instead of writing one character at a time.
The complete string is written to the file.
The fprintf() function writes formatted data to
a file. It works in a similar way to printf(), but
the output is directed to the file specified by the file pointer.
The integer and floating-point values are written to the file according to the specified format.
A file is normally opened using the "w" mode when new
contents need to be written. If the file does not exist, it is
created. If it already exists, its previous contents are replaced.
The file is opened for writing.
Before performing a write operation, the return value of
fopen() should be checked. If the file cannot be
opened, fopen() returns NULL.
This ensures that the program does not try to write using an invalid file pointer.
A string or sequence of characters can be written one character
at a time using fputc(). This approach is useful when
the program needs control over individual characters.
The characters are written individually to the file.
When the complete text is already available as a string,
fputs() can be used to write it directly to the file.
The contents of message are written to the file.
When multiple values such as names, integers and marks need to be
stored in a particular format, fprintf() is useful.
The values are written to the file in the specified order and format.
After completing all write operations, the file should be closed
using fclose(). Closing the file completes the file
operation and releases the resources associated with it.
| Function | Main Use |
|---|---|
| fputc() | Writes one character to a file. |
| fputs() | Writes a string to a file. |
| fprintf() | Writes formatted data to a file. |
| fclose() | Closes the opened file. |
Write a C program to create a text file, write a message into the
file using fputs(), and close the file.
fputs().
Open message.txt using "w" mode,
write a string using fputs(), and then close the file.
The fputs() function writes a string to the file
represented by the file pointer. The file must be opened before
writing and should be closed after the operation is complete.
Data written successfully
The program creates message.txt if it does not already
exist. The message is written using fputs() and the
file is then closed using fclose().
The fputc() function can be used when characters need
to be written individually. This is the direct counterpart of
reading individual characters using fgetc().
The characters A, B and
C are written to the file one by one.
Write a C program to write three characters into a text file using
fputc().
Characters written successfully
fputc() writes one character at a time. Multiple calls
can therefore be used to build a sequence of characters in a file.
When a file needs to store values such as student name, roll number,
and marks in a readable format, fprintf() can be used
to write all the values together.
The values are written according to the specified format.
Write a C program to store a student's name, roll number, and
percentage in a text file using fprintf().
Student record written successfully
fprintf() is useful when different data types need to
be written in a particular format. The same formatted data can
later be read using a suitable file-reading function.
| Function | What It Writes | Example |
|---|---|---|
| fputc() | One character | fputc('A', fp); |
| fputs() | String | fputs("Hello", fp); |
| fprintf() | Formatted data | fprintf(fp, "%d", number); |
| Reading | Writing |
|---|---|
fgetc() |
fputc() |
fgets() |
fputs() |
fscanf() |
fprintf() |
fgetc() → reads one character.
fputc() → writes one character.
fgets() → reads a string or line.
fputs() → writes a string.
fscanf() → reads formatted data.
fprintf() → writes formatted data.
Opening a file in "w" mode without realizing that
existing contents will be overwritten.
Forgetting to check fp == NULL before writing.
Forgetting to close the file after completing the write operation.
| Concept | Remember |
|---|---|
| Open for Writing | fopen("file.txt", "w") |
| Character | fputc() |
| String | fputs() |
| Formatted Data | fprintf() |
| Check Failure | fp == NULL |
| Close File | fclose(fp) |
fputc()?fputs()?fprintf()?fputc() and fputs()?"w" mode?fopen() be checked for NULL?fputc(), fputs() and
fprintf() with suitable examples.
Watch a beginner-friendly explanation of writing to files in C programming.
A short handwritten-style revision sheet for writing to files will be provided here.
Use the mind map for fopen(), file pointer, fputc(), fputs(), fprintf() and fclose().