File Input/Output in C


Introduction

File input/output (I/O) is a crucial aspect of C programming that allows you to read data from and write data to files. It enables data persistence and interaction with external files. In this guide, we'll explore the principles of file I/O in C and provide sample code to illustrate its usage.


Working with Files

Before we dive into examples, let's cover some key concepts related to working with files in C:

  • File Streams: In C, file operations are performed using file streams, such as
    FILE*
    pointers, which represent files being read or written.
  • Opening and Closing Files: Files must be opened before reading or writing, and they should be closed after use to release resources.
  • Reading from Files: You can read data from files using functions like
    fscanf()
    or
    fread()
    .
  • Writing to Files: You can write data to files using functions like
    fprintf()
    or
    fwrite()
    .

Sample Code

Let's explore some examples of file input/output in C:


Reading from a Text File

#include <stdio.h>
int main() {
FILE* file;
char filename[] = "sample.txt";
char buffer[100];
// Open the file for reading
file = fopen(filename, "r");
if (file == NULL) {
printf("File not found or unable to open.\\n");
return 1;
}
// Read and print the file contents
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
// Close the file
fclose(file);
return 0;
}

Writing to a Text File

#include <stdio.h>
int main() {
FILE* file;
char filename[] = "output.txt";
// Open the file for writing
file = fopen(filename, "w");
if (file == NULL) {
printf("Unable to create or open the file for writing.\\n");
return 1;
}
// Write data to the file
fprintf(file, "Hello, File I/O in C!\\n");
// Close the file
fclose(file);
return 0;
}

Binary File I/O

In addition to text files, C can handle binary files, which are used for storing non-text data. The following example demonstrates binary file I/O:


#include <stdio.h>
struct Employee {
char name[50];
int employeeId;
};
int main() {
FILE* binaryFile;
char filename[] = "employees.dat";
// Open the binary file for writing
binaryFile = fopen(filename, "wb");
if (binaryFile == NULL) {
printf("Unable to create or open the binary file for writing.\\n");
return 1;
}
// Write binary data
struct Employee employee;
strcpy(employee.name, "John Doe");
employee.employeeId = 1001;
fwrite(&employee, sizeof(struct Employee), 1, binaryFile);
// Close the binary file
fclose(binaryFile);
// Open the binary file for reading
binaryFile = fopen(filename, "rb");
if (binaryFile == NULL) {
printf("File not found or unable to open for reading.\\n");
return 1;
}
// Read binary data
fread(&employee, sizeof(struct Employee), 1, binaryFile);
printf("Name: %s\\n", employee.name);
printf("Employee ID: %d\\n", employee.employeeId);
// Close the binary file
fclose(binaryFile);
return 0;
}

Conclusion

File input/output in C is a critical skill for reading and writing data to and from files. This guide has introduced you to the key concepts and provided sample code for reading from and writing to text files, as well as working with binary files. As you continue your C programming journey, you'll use file I/O to handle data persistence, configuration files, and more.