C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In the C programming language, a file is a representation of an external device on which data can be stored, read, and manipulated. Typically, a file refers to a sequence of bytes stored on a disk, but it can also represent other types of devices such as printers or network sockets. C provides a set of standard library functions to perform various operations on files, such as opening, reading, writing, and closing.
The standard library in C includes the <stdio.h>
header, which provides the necessary functions and data types for file manipulation. Among these is the FILE
data type, which is a structure used to represent a file in C.
Here are some of the most common file manipulation functions provided by the C standard library:
fopen
: Opens a file and returns a pointer to the FILE
structure associated with it.fclose
: Closes a file associated with the provided FILE
pointer.fread
: Reads data from a file into a buffer.fwrite
: Writes data from a buffer to a file.fprintf
: Writes a formatted string to a file.fscanf
: Reads formatted data from a file.fseek
: Sets the position of the file pointer within the file.ftell
: Returns the current position of the file pointer within the file.rewind
: Sets the position of the file pointer to the beginning of the file.Example of file manipulation in C:
#include <stdio.h> int main() { // Open a file for writing FILE *file = fopen("example.txt", "w"); if (file == NULL) { printf("Error opening the file for writing.\n"); return 1; } // Write a string to the file fprintf(file, "Hello, World!\n"); // Close the file fclose(file); // Open the file for reading file = fopen("example.txt", "r"); if (file == NULL) { printf("Error opening the file for reading.\n"); return 1; } // Read the string from the file char buffer[128]; fgets(buffer, sizeof(buffer), file); // Print the read string printf("Read from the file: %s", buffer); // Close the file fclose(file); return 0; }
In this example, the program opens a file named "example.txt" for writing, writes the string "Hello, World!" to it, and then closes the file. The program then reopens the same file for reading, reads the string, prints it, and finally closes the file.
In summary, a file in the C programming language is a representation of an external device on which data can be stored, read, and manipulated. The C standard library provides various functions for file manipulation, such as fopen
, fclose
, fread
, fwrite
, and others. The FILE
data type is used to represent a file in C, and the <stdio.h>
header contains the necessary functions and data types for working with files.
File Operations in C Programming:
File operations in C involve creating, opening, reading from, writing to, and closing files.
#include <stdio.h> int main() { FILE *file; // File pointer // Perform file operations return 0; }
Creating and Opening Files in C:
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "w"); if (file != NULL) { // File creation/opening successful printf("File created/opened successfully!\n"); fclose(file); // Close the file } else { printf("Error creating/opening file!\n"); } return 0; }
Reading from Files in C:
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file != NULL) { // Reading from file char content[100]; fscanf(file, "%s", content); printf("Read from file: %s\n", content); fclose(file); // Close the file } else { printf("Error opening file for reading!\n"); } return 0; }
Writing to Files in C Programming:
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "w"); if (file != NULL) { // Writing to file fprintf(file, "Hello, File!\n"); printf("Data written to file.\n"); fclose(file); // Close the file } else { printf("Error opening file for writing!\n"); } return 0; }
Closing Files and File Pointers in C:
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "r"); if (file != NULL) { // File operations fclose(file); // Close the file } else { printf("Error opening file!\n"); } return 0; }
Error Handling in File Operations in C:
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "w"); if (file != NULL) { // File operations fclose(file); // Close the file } else { perror("Error"); } return 0; }
Binary File Handling in C Language:
#include <stdio.h> struct Person { char name[50]; int age; }; int main() { FILE *file = fopen("people.bin", "wb"); if (file != NULL) { // Binary file handling struct Person person = {"John Doe", 25}; fwrite(&person, sizeof(struct Person), 1, file); fclose(file); // Close the file } else { perror("Error"); } return 0; }