← Back to C Course | Chapter 10: File I/O | Lesson 1 of 6

C File Handling Introduction

File handling is how your program reads and writes files, like opening a notebook to read or write. You open the file, do your job, and close it.
Syntax
c
FILE *file_pointer = fopen("filename.txt", "mode");

// use fprintf, fscanf, fgets, ...

fclose(file_pointer);

Introduction to Files

This persistence is exactly why programs use files for anything that needs to survive a restart -- configuration settings, saved game progress, or logs are all stored this way rather than only living in the program's in-memory variables.

Example: Introduction to Files

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("notes.txt", "w");
	fprintf(fp, "Saved data");
	fclose(fp);
	// Print formatted text to the screen
	printf("Data persisted to a file");
	// Return 0 to signal that the program finished successfully
	return 0;
}

File Streams in C

The FILE pointer (returned by fopen()) is an opaque handle -- you never need to know its internal structure, only to pass it consistently to every file function (fread, fwrite, fclose, etc.) that operates on that particular open file.

Example: File Streams in C

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("data.txt", "w");
	fprintf(fp, "Hello");
	fclose(fp);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Standard Streams vs Files

stdin, stdout, and stderr are actually already-open FILE pointers that your program receives automatically at startup, which is why functions like printf() (which writes to stdout) don't require you to open anything first.

Example: Standard Streams vs Files

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("This goes to stdout automatically");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Basic File operations

Skipping the close step risks losing buffered data that hasn't been physically written to disk yet, since many I/O operations are buffered in memory for performance and only flushed to disk at certain points, including when the file closes.

Example: Basic File operations

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("log.txt", "w");
	fprintf(fp, "entry");
	fclose(fp);
	// Print formatted text to the screen
	printf("Closed the file");
	// Return 0 to signal that the program finished successfully
	return 0;
}

Checking file existence

This is a common lightweight pattern for existence checks, though it has a side effect: if the file does exist, you've now opened it and must remember to close it again, or you'll leak a file handle.

Example: Checking file existence

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	FILE *fp = fopen("maybe.txt", "r");
	// Check whether fp != NULL
	if (fp != NULL) {
		// Print formatted text to the screen
		printf("File exists");
		fclose(fp);
	} else {
		// Print formatted text to the screen
		printf("File does not exist");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a file without checking whether fopen returned NULL.
  2. Forgetting to close the file with fclose, so data may not be written.
  3. Using a wrong relative path, so the file is not found in the current directory.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.