139 lines
3.8 KiB
Markdown
139 lines
3.8 KiB
Markdown
File Handling
|
|
|
|
- To open a file
|
|
```c
|
|
FILE *file_pointer;
|
|
file_pointer = fopen("filename.txt", "mode");
|
|
|
|
|
|
```
|
|
"mode" specifies the purpose for opening the file, such as "r" for reading, "w" for writing, "a" for appending, "r+" for reading and writing, "w+" for reading and writing, truncating the file, etc.
|
|
- Checking if File Opened Successfully
|
|
```c
|
|
if (file_pointer == NULL) {
|
|
// File opening failed
|
|
// Handle the error
|
|
}
|
|
|
|
```
|
|
|
|
- Reading from a File
|
|
```c
|
|
// Reading using fscanf
|
|
int num;
|
|
fscanf(file_pointer, "%d", &num);
|
|
|
|
// Reading using fgets
|
|
char buffer[255];
|
|
fgets(buffer, 255, file_pointer);
|
|
```
|
|
fscanf will read just word
|
|
fgets will read whole line
|
|
|
|
- Writing to a File
|
|
|
|
Use fprintf() or fputs() to write data to the file:
|
|
```c
|
|
// Writing using fprintf
|
|
fprintf(file_pointer, "Hello, File!");
|
|
|
|
// Writing using fputs
|
|
fputs("Hello, File!", file_pointer);
|
|
```
|
|
|
|
|
|
|
|
|
|
- Closing a File
|
|
|
|
Always close the file after performing operations:
|
|
```c
|
|
fclose(file_pointer);
|
|
```
|
|
|
|
|
|
- Reading file Line by Line
|
|
```c
|
|
#define BUFFER_SIZE 1024 // Define a reasonable buffer size
|
|
|
|
FILE *file_pointer;
|
|
file_pointer = fopen("filename.txt", "r");
|
|
|
|
if (file_pointer == NULL) {
|
|
printf("File could not be opened.\n");
|
|
return -1;
|
|
}
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
while (fgets(buffer, BUFFER_SIZE, file_pointer) != NULL) {
|
|
// Remove the newline character, if present
|
|
size_t length = strlen(buffer);
|
|
if (length > 0 && buffer[length - 1] == '\n') {
|
|
buffer[length - 1] = '\0'; // Replace '\n' with '\0' (null terminator)
|
|
}
|
|
// Process the line read from the file in 'buffer'
|
|
printf("%s", buffer); // Print the line (or perform operations as needed)
|
|
}
|
|
|
|
fclose(file_pointer);
|
|
```
|
|
|
|
- When not sure about length of lines
|
|
1. Dynamic Memory Allocation:
|
|
|
|
You can dynamically allocate memory for each line by using functions like malloc() to adjust the buffer size according to the length of the read line. This method allows flexibility but requires careful memory management and error handling.
|
|
```c
|
|
FILE *file_pointer;
|
|
file_pointer = fopen("filename.txt", "r");
|
|
|
|
if (file_pointer == NULL) {
|
|
printf("File could not be opened.\n");
|
|
return -1;
|
|
}
|
|
|
|
char *buffer = NULL;
|
|
size_t buffer_size = 0;
|
|
|
|
while (getline(&buffer, &buffer_size, file_pointer) != -1) {
|
|
// Process the line read from the file in 'buffer'
|
|
printf("%s", buffer); // Print the line (or perform operations as needed)
|
|
}
|
|
|
|
free(buffer); // Free dynamically allocated memory
|
|
fclose(file_pointer);
|
|
```
|
|
|
|
|
|
|
|
2. Reading Characters One by One:
|
|
Another approach is reading characters one by one until a newline ('\n') or the end of the file is encountered. This method gives you more control over memory but may involve more code and potentially be less efficient.
|
|
```c
|
|
FILE *file_pointer;
|
|
file_pointer = fopen("filename.txt", "r");
|
|
|
|
if (file_pointer == NULL) {
|
|
printf("File could not be opened.\n");
|
|
return -1;
|
|
}
|
|
|
|
int current_char;
|
|
char buffer[MAX_LINE_LENGTH]; // Define a maximum line length
|
|
|
|
while ((current_char = fgetc(file_pointer)) != EOF) {
|
|
if (current_char == '\n' || current_char == EOF) {
|
|
// Process the line read from the file in 'buffer'
|
|
printf("%s\n", buffer); // Print the line (or perform operations as needed)
|
|
memset(buffer, 0, sizeof(buffer)); // Clear buffer for the next line
|
|
} else {
|
|
// Append character to buffer
|
|
// Ensure buffer is not full to avoid overflow
|
|
// Handle buffer overflow scenario
|
|
}
|
|
}
|
|
|
|
fclose(file_pointer);
|
|
```
|
|
These are basic examples to illustrate the concept. The first method using getline() might be more convenient, while the second method gives you finer control over memory but involves more intricate handling. Adjust these examples according to your specific requirements and error handling needs.
|
|
|
|
|