Start your programming journey with one of the most powerful and foundational languages — C. This comprehensive course is designed for absolute beginners as well as intermediate learners who want to build a solid understanding of programming using the C language. Whether you're preparing for college-level programming, cracking technical interviews, or planning to explore systems or embedded development, this course covers everything step-by-step. Through hands-on examples, real-world practice problems, and structured explanations, you’ll learn how to write clean and efficient C code — from your first printf() to advanced data structures and memory management.
File handling in C allows a program to perform input and output with files stored on disk. Instead of reading or writing from the terminal, you can work with permanent data using FILE operations like fopen(), fclose(), fprintf(), fscanf(), fread(), and fwrite().
fopen() opens a file and returns a pointer to a FILE structure.
FILE *fp;
fp = fopen("data.txt", "r"); // open for reading
| Mode | Description |
|---|---|
| "r" | Read (file must exist) |
| "w" | Write (create or truncate) |
| "a" | Append to file |
| "r+" | Read + Write |
FILE *fp = fopen("notes.txt", "w");
fprintf(fp, "Welcome to UdaanPath!");
fclose(fp);
char text[100];
FILE *fp = fopen("notes.txt", "r");
fscanf(fp, "%s", text);
printf("Read: %s", text);
fclose(fp);
#include <stdio.h>
int main() {
FILE *log = fopen("udaanpath_users.txt", "a");
if (log == NULL) {
printf("File not found!\n");
return 1;
}
char name[50];
printf("Enter user name: ");
scanf("%s", name);
fprintf(log, "New user: %s\n", name);
fclose(log);
printf("User %s added successfully!\n", name);
return 0;
}
Always close the file using fclose(fp) after reading/writing to free up system resources.
C also supports binary file operations using fread() and fwrite().
fwrite(&student, sizeof(student), 1, fp);
fread(&student, sizeof(student), 1, fp);
FILE * to create a file pointer.fopen() opens, fclose() closes the file.fprintf() writes, fscanf() reads formatted data.fopen() returned NULL before file operations.In the next chapter, we’ll understand how to handle Command Line Arguments in C — passing arguments to your program via the terminal.