Home Assignment -4
CO4
22CA1101OJ- Problem Solving Through Programming
1. Explain various types of file operations in C language? Discuss with suitable
examples?
In C programming, file operations are performed using file handling functions and structures
provided by the standard library. There are several types of file operations that can be
performed, including creating, opening, reading, writing, and closing files. Let's discuss each
of these file operations with suitable examples:
1. Creating a File:
To create a new file, you can use the `fopen()` function with the "w" mode (write mode) as
the second parameter. If the file does not exist, it will be created. If it already exists, its
contents will be truncated.
Example:
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file != NULL) {
printf("File created successfully.\n");
fclose(file);
} else {
printf("Error in creating the file.\n");
}
return 0;
}
```
2. Opening a File:
To open an existing file, you can use the `fopen()` function with the appropriate mode ("r" for
read, "w" for write, "a" for append, etc.). If the file is successfully opened, the `fopen()`
function returns a pointer to a `FILE` structure.
Example:
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file != NULL) {
printf("File opened successfully.\n");
fclose(file);
} else {
printf("Error in opening the file.\n");
}
return 0;
}
```
3. Reading from a File:
To read data from a file, you can use functions like `fscanf()` or `fgets()` in combination with
a `FILE` pointer. These functions allow you to read data from the file into variables or
character arrays.
Example:
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file != NULL) {
char data[100];
fscanf(file, "%s", data);
printf("Data read from the file: %s\n", data);
fclose(file);
} else {
printf("Error in opening the file.\n");
}
return 0;
}
```
4. Writing to a File:
To write data to a file, you can use functions like `fprintf()` or `fputs()` in combination with a
`FILE` pointer. These functions allow you to write data to the file from variables or character
arrays.
Example:
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "w");
if (file != NULL) {
char data[] = "Hello, world!";
fprintf(file, "%s", data);
printf("Data written to the file.\n");
fclose(file);
} else {
printf("Error in opening the file.\n");
}
return 0;
}
```
5. Closing a File:
After performing file operations, it is important to close the file using the `fclose()` function.
This ensures that any changes made to the file are saved and system resources are freed.
Example:
```c
#include <stdio.h>
int main() {
FILE *file = fopen("example.txt", "r");
if (file != NULL) {
// Perform file operations
fclose(file);
printf("File closed successfully.\n");
} else {
printf("Error in opening the file.\n");
}
return 0;
}
```
It's important to note that these examples illustrate basic file operations, and error handling
should be implemented for robust file handling in