File Input and Output in C
File Handling in C
File input and output functions are provided in the C standard library through the stdio.h header file. These functions allow you to read data from a file or write data to a file in your C program.
Files are used to store data in a persistent manner, which means that the data remains on the storage device even after the program that created the data has terminated. This is useful for storing data that needs to be accessed later or data that is too large to fit in memory all at once.
Why files are needed?
There are several types of files that are commonly used, including text files, binary files, and media files (such as images, audio, and video). Text files are used to store data in a human-readable format, while binary files are used to store data in a machine-readable format. Files are often used to store data that needs to be shared between different programs or devices.
In C programming, file input and output are the processes of reading from and writing to files, respectively.
Operations in file handling
In C programming, there are several operations that can be performed on files. These operations include opening and closing files, reading from and writing to files, and seeking a specific position within a file. Here are some of the most commonly used file input and output functions in C:
fopen()
: Opens a file and returns a pointer to a FILE type, which is a structure that represents an open file.fclose()
: Closes an open file.fgetc()
: Reads a single character from a file.fgets()
: Reads a line of text from a file.fprintf()
: Writes formatted output to a file.fputc()
: Writes a single character to a file.fputs()
: Writes a string to a file.
To use these functions, you need to include the stdio.h header file in your C program and use the syntax provided by each function.
Working with files in C
To work with files in C, you need to use the file input and output functions provided in the C standard library through the stdio.h header file. These functions allow you to read from and write to files in your C program.
Here are the steps to follow when working with files in C:
- Include the stdio.h header file at the top of your C program.
- Declare a pointer to a FILE type. This pointer will be used to refer to the open file.
- Use the
fopen()
function to open the file. This function takes the name of the file and the mode in which the file should be opened (such as “r” for reading or “w” for writing) as arguments, and returns a pointer to a FILE type. - Check if the file was successfully opened. If the
fopen()
function returnsNULL
, it means that the file could not be opened. - Use the file input and output functions to read from or write to the file. Some of the most commonly used functions are
fgetc()
for reading a single character,fgets()
for reading a line of text,fprintf()
for writing formatted output, andfputc()
andfputs()
for writing a single character or a string, respectively. - Use the
fclose()
function to close the file when you are done. This releases any resources that were allocated for the file.
#include <stdio.h> int main() { // Declare a file pointer FILE *fp; // Open the file for reading fp = fopen("input.txt", "r"); // Check if the file was successfully opened if (fp == NULL) { printf("Error opening file\n"); return 1; } // Declare a character array to store the contents of the file char buffer[100]; // Read the contents of the file into the buffer fgets(buffer, 100, fp); // Print the contents of the buffer printf("%s", buffer); // Close the file fclose(fp); return 0; }
Output
Error Opening File
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment