C File Input and Output I/O

File I/O in C refers to the process of reading from and writing to files on a computer. File I/O is an important aspect of programming, as it allows programs to persist data beyond the life of the program itself.

In C, file I/O is performed using a set of functions provided by the standard library. The main functions used for file I/O in C are fopen, fclose, fread, fwrite, fseek, and ftell.

Creating a File in C

To create a new file in C, you can use the fopen function. The fopen function takes two arguments: the name of the file to be created and the mode in which the file should be opened.

The following code demonstrates how to create a new file in C using the fopen function:

1#include <stdio.h> 2 3int main() 4{ 5 FILE *fp; 6 fp = fopen("newfile.txt", "w"); 7 if (fp == NULL) 8 { 9 printf("Error opening file\n"); 10 return 1; 11 } 12 printf("File created successfully\n"); 13 fclose(fp); 14 return 0; 15}

The fopen function returns a pointer to a FILE structure, which is used for subsequent file I/O operations. The mode argument, "w", specifies that the file should be opened for writing. If the file already exists, its contents will be overwritten.

Reading from a File in C

To read data from a file in C, you can use the fread function. The fread function takes four arguments: a pointer to the buffer where the data should be stored, the size of each element to be read, the number of elements to be read, and a pointer to the file to be read.

The following code demonstrates how to read data from a file in C using the fread function:

1#include <stdio.h> 2 3int main() 4{ 5 FILE *fp; 6 char buffer[100]; 7 fp = fopen("newfile.txt", "r"); 8 if (fp == NULL) 9 { 10 printf("Error opening file\n"); 11 return 1; 12 } 13 fread(buffer, sizeof(char), 100, fp); 14 printf("Data read from file: %s\n", buffer); 15 fclose(fp); 16 return 0; 17}

In this example, the fopen function is used to open the file "newfile.txt" in read mode. The fread function is then used to read 100 characters of data from the file into the buffer buffer. The contents of the buffer are then printed to the console.

Writing to a File in C

To write data to a file in C, you can use the fwrite function. The fwrite function takes four arguments: a pointer to the buffer containing the data to be written, the size of each element to be written, the number of elements to be written, and a pointer to the file to be written.

The following code demonstrates how to write data to a file in C using the fwrite function:

1#include <stdio.h> 2 3int main() 4{ 5 FILE *fp; 6 char data[] = "Hello, World!"; 7 fp = fopen("newfile.txt", "a"); 8 if (fp == NULL) 9 { 10 printf("Error opening file\n"); return 1; 11 } 12 fwrite(data, sizeof(char), sizeof(data), fp); 13 printf("Data written to file\n"); 14 fclose(fp); 15 return 0; 16}

In this example, the fopen function is used to open the file "newfile.txt" in append mode. The fwrite function is then used to write the contents of the buffer data to the file. The contents of the file can be verified by reading the file and printing its contents to the console.