glibc fopen
时间: 2023-08-13 07:08:46 浏览: 61
glibc is the GNU C Library, which provides essential functions for C programming language. One of the functions provided by glibc is `fopen()`.
`fopen()` is used to open a file and returns a `FILE` pointer that can be used to read from or write to the file. It takes two arguments: a filename and a mode string specifying the file access mode. The mode string can include characters like "r" for reading, "w" for writing, "a" for appending, and more.
Here's an example of how you can use `fopen()` in C:
```c
#include <stdio.h>
int main() {
FILE *file;
char *filename = "example.txt";
char *mode = "r";
file = fopen(filename, mode);
if (file == NULL) {
printf("Failed to open the file.\n");
return 1;
}
// Do something with the file
fclose(file);
return 0;
}
```
In this example, `fopen()` is used to open the file "example.txt" in read mode. If the file opening fails, it prints an error message. After performing operations on the file, it is important to close the file using `fclose()` to release the resources associated with it.