get file contents by c languige
时间: 2024-02-21 20:02:20 浏览: 76
解析file_get_contents模仿浏览器头(user_agent)获取数据
To read the contents of a file in C language, you can use the following code:
```c
#include <stdio.h>
int main() {
FILE *fp;
char ch;
// Open file
fp = fopen("filename.txt", "r");
// Check if file exists
if (fp == NULL) {
printf("Cannot open file \n");
return 1;
}
// Read contents from file
while ((ch = fgetc(fp)) != EOF) {
printf("%c", ch);
}
// Close file
fclose(fp);
return 0;
}
```
In the above code, replace `filename.txt` with the name of the file you want to read. The `fopen` function opens the file in read mode, and the `fgetc` function reads each character of the file until the end of file (EOF) is reached. The `fclose` function closes the file.
阅读全文