它正在工作,但例如当我写str1时,它应该是"我们"但这并不是真实的写法。相反,它写了一个不同的形状。
#include<stdio.h>
#include<conio.h>
#include <string.h>
#include<stdlib.h>
int main()
{
char str1[10], str2[10], str3[10];
int year;
FILE* fp;
fp = fopen("a.txt", "w+") == NULL;
fputs("We are in 2012", fp);
rewind(fp);
//fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
fscanf(fp, "%s", str1);
printf("Read String1 |%s|n", str1);
//printf("Read String2 |%s|n", str2);
//printf("Read String3 |%s|n", str3);
//printf("Read Integer |%d|n", year);
system("pause");
return(0);
}
fp = fopen("a.txt", "w+") == NULL;
将比较结果赋值给fp
。
移除== NULL
以赋值fopen()
自身返回的文件指针
fp = fopen("a.txt", "w+");
之后,您应该使用if
语句检查fp
是否不是NULL
。
if (fp == NULL) {
perror("fopen");
return 1;
}