Tutorials Home   >   Practical Programming Skills   >   Writing to Files

Writing to Files

1. Introduction

Writing to files in C means storing data in a file so that it can be used later. Unlike variables, file data is stored permanently on secondary storage such as a hard disk. File writing is very useful when a program needs to save results, records, or reports.

Writing to files is widely used in:

  • Student and employee record systems

  • Billing and accounting software

  • Log files and reports

  • Database applications

C provides several built-in functions to write data into files efficiently.


2. Steps to Write to a File in C

The following steps are followed to write data to a file:

  1. Declare a file pointer

  2. Open the file in write or append mode

  3. Write data using file writing functions

  4. Close the file


3. File Pointer

A file pointer is required to connect the program with a file.

Declaration

FILE *fp;
  • FILE is a structure defined in stdio.h

  • fp stores information about the file


4. Opening a File for Writing

To write data into a file, we use the fopen() function.

Syntax

fp = fopen("filename", "mode");

Common Writing Modes

Mode Description
"w" Write mode (creates new file / deletes old data)
"a" Append mode (adds data at the end)
"w+" Read and write
"a+" Read and append

Example

fp = fopen("data.txt", "w");

âš  Important:

  • "w" mode erases old data

  • "a" mode preserves old data


5. Writing Data Using fprintf()

Description

  • Writes formatted output to a file

  • Works like printf() but writes to a file

Syntax

fprintf(fp, "format", variables);

Example Program

#include <stdio.h>

int main() {
FILE *fp;
fp = fopen(“student.txt”, “w”);

fprintf(fp, “Name: Ravi\n”);
fprintf(fp, “Marks: 88\n”);

fclose(fp);
return 0;
}

Use

  • Best for writing text and formatted data


6. Writing Data Using fputc()

Description

  • Writes one character at a time

Syntax

fputc(character, fp);

Example

#include <stdio.h>

int main() {
FILE *fp;
char ch = ‘A’;

fp = fopen(“char.txt”, “w”);
fputc(ch, fp);

fclose(fp);
return 0;
}

Use

  • Character-by-character writing


7. Writing Data Using fputs()

Description

  • Writes a string into a file

Syntax

fputs("string", fp);

Example Program

#include <stdio.h>

int main() {
FILE *fp;

fp = fopen(“message.txt”, “w”);
fputs(“Welcome to File Handling in C”, fp);

fclose(fp);
return 0;
}

Advantage

  • Simple and fast for strings


8. Writing Multiple Lines to a File

Example Program

#include <stdio.h>

int main() {
FILE *fp;
fp = fopen(“notes.txt”, “w”);

fputs(“File Handling in C\n”, fp);
fputs(“Writing to Files\n”, fp);
fputs(“End of File\n”, fp);

fclose(fp);
return 0;
}


9. Writing Data Using Append Mode

Append mode adds data at the end of the file.

Example

fp = fopen("data.txt", "a");
fprintf(fp, "New record added\n");

✔ Old data remains safe
✔ New data is added at the end


10. Writing to Binary Files

Binary files store data in binary format, not readable by humans.

Function Used: fwrite()

Syntax

fwrite(&variable, sizeof(variable), 1, fp);

Example: Writing a Structure to a Binary File

#include <stdio.h>

struct student {
int roll;
float marks;
};

int main() {
FILE *fp;
struct student s = {101, 92.5};

fp = fopen(“data.bin”, “wb”);
fwrite(&s, sizeof(s), 1, fp);

fclose(fp);
return 0;
}


11. Importance of Closing a File

After writing data, the file must be closed using fclose().

Syntax

fclose(fp);

Reasons

  • Saves data properly

  • Frees memory

  • Prevents data loss


12. Comparison of File Writing Functions

Function Writes Best For
fprintf() Formatted text Text files
fputc() Single character Character writing
fputs() String Line writing
fwrite() Binary data Binary files

13. Common Errors While Writing to Files

  • Opening file in wrong mode

  • Forgetting to close the file

  • Using invalid file pointer

  • Overwriting data accidentally


14. Advantages of Writing to Files

  • Permanent storage of data

  • Useful for large data handling

  • Data can be reused later

  • Helps in creating reports and records


15. Conclusion

Writing to files in C allows programs to store data permanently. By using file pointers and file writing functions like fprintf(), fputc(), fputs(), and fwrite(), data can be stored efficiently in both text and binary files. Understanding file writing is essential for developing real-world C applications.