In C, file operations are performed using the standard C library’s file input/output (I/O) functions. These functions allow you to create, open, read, write, and close files. The most commonly used file I/O functions in C are `fopen()`, `fclose()`, `fread()`, `fwrite()`, `fprintf()`, and `fscanf()`.
1. `fopen()` function: This function is used to open a file. The syntax for using `fopen()` is:
FILE *fopen(const char *filename, const char *mode);
Here, `filename` is a string that contains the name of the file to be opened, and `mode` is a string that specifies the mode in which the file is to be opened (e.g., read, write, or append). The return value is a pointer to a `FILE` structure that represents the opened file.
For example, to open a file named “myfile.txt” for writing, we can use the following code:
FILE *fp; fp = fopen("myfile.txt", "w");
2. `fclose()` function: This function is used to close a file that was previously opened using `fopen()`. The syntax for using `fclose()` is:
int fclose(FILE *fp);
Here, `fp` is a pointer to the `FILE` structure that represents the file to be closed. The return value is an integer that indicates whether the operation was successful (zero) or not (non-zero).
For example, to close the file that we opened in the previous example, we can use the following code:
fclose(fp);
3. `fread()` function: This function is used to read data from a file. The syntax for using `fread()` is:
size_t fread(void *ptr, size_t size, size_t count, FILE *fp);
Here, `ptr` is a pointer to the buffer where the data is to be stored, `size` is the size of each element to be read, `count` is the number of elements to be read, and `fp` is a pointer to the `FILE` structure that represents the file to be read. The return value is the number of elements read.
For example, to read 10 integers from a file into an array, we can use the following code:
int array[10]; FILE *fp; fp = fopen("myfile.txt", "r"); fread(array, sizeof(int), 10, fp); fclose(fp);
4. `fwrite()` function: This function is used to write data to a file. The syntax for using `fwrite()` is:
size_t fwrite(const void *ptr, size_t size, size_t count, FILE *fp);
Here, `ptr` is a pointer to the buffer containing the data to be written, `size` is the size of each element to be written, `count` is the number of elements to be written, and `fp` is a pointer to the `FILE` structure that represents the file to be written. The return value is the number of elements written.
For example, to write an array of 10 integers to a file, we can use the following code:
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; FILE *fp; fp = fopen("myfile.txt", "w"); fwrite(array, sizeof(int), 10, fp); fclose(fp);
5. `fprintf()` function: This function is used to write formatted data to a file. The syntax for using `fprintf()` is:
int fprintf(FILE *fp, const char *format, ...);
Here, `fp` is a pointer to the `FILE` structure that represents the file to be written, `format` is a string that specifies the format of the output, and `…` are the arguments to be formatted and written to the file. The return value is the number of characters written.
For example, to write a formatted string to a file, we can use the following code:
FILE *fp; fp = fopen("myfile.txt", "w"); fprintf(fp, "%s %d", "The answer is", 42); fclose(fp);
6. `fscanf()` function: This function is used to read formatted data from a file. The syntax for using `fscanf()` is:
int fscanf(FILE *fp, const char *format, ...);
Here, `fp` is a pointer to the `FILE` structure that represents the file to be read, `format` is a string that specifies the format of the input, and `…` are the pointers to the variables that will store the input. The return value is the number of input items successfully matched and assigned.
For example, to read a formatted string and an integer from a file, we can use the following code:
FILE *fp; fp = fopen("myfile.txt", "r"); char str[50]; int num; fscanf(fp, "%s %d", str, &num); fclose(fp);
It’s important to note that when using file I/O functions in C, you should always check the return values to ensure that the operations were successful, and to handle any errors appropriately.