C Language Interview: File Operations and Memory Management
In the world of C programming, file operations and memory management are two crucial topics. Whether you are handling data, saving application states, or optimizing memory usage in high-performance computing, these two concepts are fundamental and indispensable. In this article, we will provide a detailed overview of these two topics, accompanied by code demonstrations to help basic users gain a deeper understanding.
1. File Operations
1. File Pointers
C uses the <span>FILE</span>
structure to represent files, and we manipulate it through file pointers. The standard library <span><stdio.h></span>
provides various functions to open, read, write, and close files.
#include <stdio.h>
int main() { FILE *file; // Define a FILE pointer
file = fopen("example.txt", "w"); // Open or create a text file for writing if (file == NULL) { printf("Cannot open or create file!\n"); return 1; }
fprintf(file, "Hello, World!\n"); // Write content to the file fclose(file); // Close the file
return 0;}
File Mode Descriptions:
<span>"r"</span>
: Read-only mode, opens an existing text file.<span>"w"</span>
: Write mode, creates a new text file, clearing the content if it already exists.<span>"a"</span>
: Append mode, adds new content after existing content.<span>"rb"</span>
,<span>"wb"</span>
,<span>"ab"</span>
, etc.: Used for binary mode.
2. File Reading Example
The following code demonstrates how to read data from a text file:
#include <stdio.h>
int main() { FILE *file; char buffer[100];
file = fopen("example.txt", "r"); // Open the text file for reading if (file == NULL) { printf("Cannot open the document!\n"); return 1; }
while (fgets(buffer, sizeof(buffer), file)) { // Read line by line from the beginning printf("%s", buffer); }
fclose(file); // Close the document after reading
return 0;}
2. Memory Management
In C, you need to manually allocate and free memory, which is a significant characteristic compared to other high-level programming languages. All dynamic memory operations are included in the <span><stdlib.h></span>
library.
Examples of malloc and free functions:
<span>malloc()</span>
is used to dynamically allocate a block of memory of a specified size, returning a void pointer that needs to be cast to the target type.<span>free()</span>
is used to release previously allocated memory to avoid memory leaks.
#include <stdio.h>
#include <stdlib.h>
int main() { int n, i;
printf("Please enter the number of integers: "); scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int)); // Dynamically allocate an integer array
if (arr == NULL) { printf("Memory allocation failed!\n"); return -1; }
for(i = 0; i < n; i++) { arr[i] = i + 1; }
for(i = 0; i < n; i++) { printf("%d ", arr[i]); }
free(arr); // Free the allocated object
return 0;}
3. Precautions
Memory Leak Detection:
Every time you use <span>malloc</span>
or <span>calloc</span>
to allocate dynamic memory, you must use <span>free()</span>
to release it to avoid wasting resources.
Safety:
Always verify the success of input and output operations to ensure the program does not crash during execution. Additionally, prevent security risks such as buffer overflows by using <span>fgets()</span>
instead of lower-level character arrays (<span>scanf()</span><span>). </span>
Conclusion
Familiarity with the basic knowledge of process-oriented and flexible control in C can effectively solve many practical problems in various environments. Future program development and maintenance will also become easier.
I hope this article helps you further understand and master the knowledge related to file operations and memory management in C, preparing you for upcoming interviews.