FILE *open( const char *restrict path, const char *restrict mode )
int fclose(FILE *stream);
fscanf(FILE*, ...)
fprintf(FILE*, ...)
/*
打开文件的标准代码
FILE *fp = fopen("file", "r"); // file = a.txt
if ( fp ) {
fscanf(fp, ...);
fclose(fp);
} else {
...
}
*/
怎样用fopen呢
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("12.txt", "w");
if ( fp ) {
int a = 15;
fscanf(fp, "%d", &a);
// fprintf(fp);
fclose(fp);
} else {
printf("file can't open\n");
}
return 0;
}
fopen
r : 打开只读
r+ : 打开读写,从文件头开始
w : 打开只写,不存在新建,若文件存在则清空
w+ : 打开读写,不存在新建,若文件存在则清空
a : 打开追加,如果不存在新建,存在则从文件尾开始
..x : 只新建,如文件存在,则不能打开
src from: 翁男神