C Programming
Master the fundamentals of C programming with comprehensive tutorials, examples, and hands-on exercises
File Handling in C
File handling allows programs to store data permanently and read data from external files. C provides several functions for file operations through the stdio.h library.
Opening and Closing Files
Basic File Operations
#include <stdio.h>
int main() {
FILE *file;
// Open file for writing
file = fopen("example.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
// Write to file
fprintf(file, "Hello, World!\n");
fprintf(file, "This is a test file.\n");
// Close file
fclose(file);
printf("File written successfully.\n");
return 0;
}
Reading from Files
File Reading Methods
#include <stdio.h>
int main() {
FILE *file;
char buffer[100];
// Open file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
printf("File contents:\n");
// Method 1: Read line by line
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
// Reset file pointer to beginning
rewind(file);
printf("\nReading character by character:\n");
// Method 2: Read character by character
int ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
File Modes
Different File Modes
#include <stdio.h>
int main() {
FILE *file;
// Write mode - creates new file or overwrites existing
file = fopen("test1.txt", "w");
fprintf(file, "Write mode\n");
fclose(file);
// Append mode - adds to end of existing file
file = fopen("test1.txt", "a");
fprintf(file, "Append mode\n");
fclose(file);
// Read mode - opens existing file for reading
file = fopen("test1.txt", "r");
char buffer[100];
printf("File contents after append:\n");
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
// Read and write mode
file = fopen("test2.txt", "w+");
fprintf(file, "Read and write mode\n");
rewind(file); // Go back to beginning
fgets(buffer, sizeof(buffer), file);
printf("Read back: %s", buffer);
fclose(file);
return 0;
}
Binary File Operations
Binary File Handling
#include <stdio.h>
struct Student {
int id;
char name[50];
float gpa;
};
int main() {
FILE *file;
struct Student students[3] = {
{1, "Alice", 3.8},
{2, "Bob", 3.5},
{3, "Charlie", 3.9}
};
// Write binary data
file = fopen("students.dat", "wb");
if (file == NULL) {
printf("Error creating file!\n");
return 1;
}
fwrite(students, sizeof(struct Student), 3, file);
fclose(file);
// Read binary data
file = fopen("students.dat", "rb");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
struct Student readStudents[3];
fread(readStudents, sizeof(struct Student), 3, file);
fclose(file);
printf("Students read from binary file:\n");
for (int i = 0; i < 3; i++) {
printf("ID: %d, Name: %s, GPA: %.1f\n",
readStudents[i].id,
readStudents[i].name,
readStudents[i].gpa);
}
return 0;
}
File Mode Reference:
- "r" - Read only
- "w" - Write only (overwrites)
- "a" - Append only
- "r+" - Read and write
- "w+" - Read and write (overwrites)
- "a+" - Read and append
- Add "b" for binary mode (e.g., "rb", "wb")
Frequently Asked Questions
C is a powerful, general-purpose programming language that's been around since the 1970s. It's the foundation for many modern languages and is essential for system programming, embedded systems, and understanding how computers work at a low level. Learning C gives you a solid foundation in programming fundamentals.
No prior programming experience is required! Our C programming tutorial is designed for complete beginners. We start with the basics and gradually progress to more advanced topics. However, having some basic computer literacy and logical thinking skills will be helpful.
You need a C compiler to run C programs. For beginners, we recommend using our online C compiler which requires no installation. For local development, you can use GCC (GNU Compiler Collection) which is available on Windows (via MinGW), macOS (via Xcode), and Linux. IDEs like Code::Blocks, Dev-C++, or Visual Studio Code are also helpful.
The time to learn C depends on your dedication and prior experience. With consistent practice (1-2 hours daily), you can grasp the basics in 2-4 weeks and become proficient in 2-3 months. Mastering advanced concepts like pointers, memory management, and data structures may take 6-12 months of regular practice.
C programming skills open doors to various career paths including system programming, embedded systems development, operating system development, device driver programming, game development, and firmware development. Many companies in automotive, aerospace, telecommunications, and IoT sectors actively seek C programmers.
Absolutely! C remains highly relevant and is consistently ranked among the top programming languages. It's essential for system programming, embedded systems, IoT devices, and performance-critical applications. Many modern languages are built on C, and understanding C helps you become a better programmer overall.