๐Ÿ“‚ C File Handling
Estimated reading: 3 minutes 7 views

๐Ÿ“ C File I/O Overview โ€“ Performing File Input and Output in C


๐Ÿงฒ Introduction โ€“ What Is File I/O in C?

File I/O (Input/Output) in C allows programs to interact with external filesโ€”such as reading input from a file instead of the keyboard, or writing output to a file instead of the screen. It is part of the Standard I/O Library (<stdio.h>) and enables long-term data storage, report generation, logging, and data transfer.

๐ŸŽฏ In this overview, youโ€™ll learn:

  • How C handles file input and output
  • Key file functions and file modes
  • Differences between file and standard console I/O
  • How to open, read, write, and close files

๐Ÿ” Core Concept โ€“ The FILE Pointer

C handles files using the built-in FILE data type, which represents a file stream. Before performing I/O, you must associate a file stream with an actual file using the fopen() function.

FILE *fp = fopen("data.txt", "r");  // Open file for reading

๐Ÿ“˜ fp is a pointer to a file stream. If the file can’t be opened, fopen() returns NULL.


๐Ÿ“‚ File Opening Modes

You can open files in various modes depending on whether you want to read, write, or append data, and whether the file is text or binary.

ModeDescription
"r"Read from existing text file
"w"Write to text file (overwrite if exists)
"a"Append to text file
"r+"Read & write from existing text file
"w+"Read & write to new/overwrite text file
"a+"Read & append to text file
"rb", "wb", "ab"Binary file equivalents

๐Ÿ’ป Key File I/O Functions in C

FunctionPurpose
fopen()Opens a file
fclose()Closes a file
fprintf()Writes formatted output to file
fscanf()Reads formatted input from file
fgets()Reads a string/line from file
fputs()Writes a string to file
fread()Reads binary data
fwrite()Writes binary data
feof()Checks end of file
ferror()Checks file error state

โœ๏ธ Writing to a File โ€“ Example

FILE *fp = fopen("log.txt", "w");
fprintf(fp, "Program started\n");
fclose(fp);

๐Ÿ“– Reading from a File โ€“ Example

FILE *fp = fopen("input.txt", "r");
char line[100];
while (fgets(line, sizeof(line), fp)) {
    printf("%s", line);
}
fclose(fp);

โ— Validating File Operations

Always check for file open errors:

FILE *fp = fopen("config.txt", "r");
if (fp == NULL) {
    perror("Error opening file");
}

๐Ÿ“ฆ Text vs Binary File Modes

  • Text mode: human-readable data (.txt, .csv)
  • Binary mode: raw byte data (.bin, .dat)

Choose based on use caseโ€”text for logs/configs, binary for structured/raw data.


๐Ÿ“Œ Summary โ€“ Recap & Next Steps

C’s File I/O system allows seamless interaction with files for persistent data handling. Using file streams and standard I/O functions, you can open, process, and manage files safely and efficiently.

๐Ÿ” Key Takeaways:

  • Use FILE * with fopen() to access files
  • Choose correct mode ("r", "w", "rb", etc.)
  • Use fprintf(), fscanf() for text, fwrite(), fread() for binary
  • Always check if fopen() succeeded before proceeding
  • Close files with fclose() to avoid resource leaks

โš™๏ธ Real-World Relevance:

Used in report generation, configuration reading, data storage, game saves, system logs, and file parsers.


โ“ Frequently Asked Questions (FAQ)

โ“ What is a FILE * in C?

โœ… Itโ€™s a pointer to a file stream used to read from or write to a file.


โ“ How do I open a file in C?

โœ… Use fopen("filename.txt", "mode") where mode can be "r", "w", "a", etc.


โ“ Whatโ€™s the difference between fscanf and fgets?

โœ… fscanf() reads formatted input; fgets() reads a line or string safely.


โ“ Why should I close a file after use?

โœ… To flush buffers, release system resources, and prevent data loss.


โ“ How can I check for end-of-file?

โœ… Use feof(FILE *fp) which returns a non-zero value when EOF is reached.


Share Now :

Leave a Reply

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

Share

๐Ÿ“ C File I/O Overview

Or Copy Link

CONTENTS
Scroll to Top