Reading and Writing Files in C
Introduction
File handling is an essential concept in programming that allows you to store and retrieve data from files on disk. In the C programming language, file handling is done using a set of standard library functions provided by the stdio.h
header file. In this tutorial, we will explore how to read from and write to files in C.
Opening and Closing Files
Before reading from or writing to a file, you need to open it using the fopen()
function. This function returns a pointer to a FILE
object, which you will use to interact with the file. After you are done with the file, you should close it using the fclose()
function to free up resources.
Example:
#include <stdio.h> int main() { FILE *file; // Open a file for reading file = fopen("example.txt", "r"); if (file == NULL) { perror("Error opening file"); return -1; } // Close the file fclose(file); return 0; }
Reading from a File
To read data from a file, you can use various functions such as fgetc()
, fgets()
, and fread()
.
Using fgetc()
The fgetc()
function reads a single character from a file.
Example:
#include <stdio.h> int main() { FILE *file; char ch; file = fopen("example.txt", "r"); if (file == NULL) { perror("Error opening file"); return -1; } while ((ch = fgetc(file)) != EOF) { putchar(ch); } fclose(file); return 0; }
Using fgets()
The fgets()
function reads a string from a file.
Example:
#include <stdio.h> int main() { FILE *file; char buffer[100]; file = fopen("example.txt", "r"); if (file == NULL) { perror("Error opening file"); return -1; } while (fgets(buffer, sizeof(buffer), file) != NULL) { printf("%s", buffer); } fclose(file); return 0; }
Using fread()
The fread()
function reads a block of data from a file.
Example:
#include <stdio.h> int main() { FILE *file; char buffer[100]; size_t bytesRead; file = fopen("example.txt", "rb"); if (file == NULL) { perror("Error opening file"); return -1; } bytesRead = fread(buffer, sizeof(char), sizeof(buffer), file); fwrite(buffer, sizeof(char), bytesRead, stdout); fclose(file); return 0; }
Writing to a File
To write data to a file, you can use functions such as fputc()
, fputs()
, and fwrite()
.
Using fputc()
The fputc()
function writes a single character to a file.
Example:
#include <stdio.h> int main() { FILE *file; file = fopen("output.txt", "w"); if (file == NULL) { perror("Error opening file"); return -1; } fputc('A', file); fclose(file); return 0; }
Using fputs()
The fputs()
function writes a string to a file.
Example:
#include <stdio.h> int main() { FILE *file; file = fopen("output.txt", "w"); if (file == NULL) { perror("Error opening file"); return -1; } fputs("Hello, World!\n", file); fclose(file); return 0; }
Using fwrite()
The fwrite()
function writes a block of data to a file.
Example:
#include <stdio.h> int main() { FILE *file; char buffer[] = "Hello, World!\n"; file = fopen("output.txt", "wb"); if (file == NULL) { perror("Error opening file"); return -1; } fwrite(buffer, sizeof(char), sizeof(buffer) - 1, file); fclose(file); return 0; }
Conclusion
In this tutorial, we have covered the basics of file handling in C, including opening and closing files, reading from files, and writing to files. By mastering these concepts, you can efficiently manage data storage and retrieval in your C programs.