C语言问题,文件中的那个的内容换行了之后怎么读?

#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE*fpin,*fpout;
double f=1,d;
char str[]="thisis\nastring",str1[40],str2[256];
fpin=fopen("open.txt","w");
if(!fpin)
{
printf("打开文件失败\n");
return 0;
}
fprintf(fpin,"%s\n%lf",str,f);
fclose(fpin);
fpout=fopen("open.txt","r");
fscanf(fpout,"%s",str1);
fgets(str2,200,fpout);
fscanf(fpout,"%lf",&d);
printf("%s,%s\n%lf",str1,str2,d);
fclose(fpout);
return 0;
}
为什么总读出来乱码?
下面是对应的文件!

//fscanf(fpin,"%s\n",str1); // 格式串中加一个'\n',否则会留给fgets()

 

#include <stdio.h>
#include <stdlib.h>

int main() {
FILE *fpin,*fpout;
double f = 1,d;
char str[] = "thisis\nastring",str1[40],str2[256];
fpout = fopen("open.txt","w");
if(!fpout) {
printf("打开文件失败\n");
return 0;
}
fprintf(fpout,"%s\n%lf\n",str,f);
fclose(fpout);

fpin = fopen("open.txt","r");
fscanf(fpin,"%s\n",str1); // 就这儿有问题
fgets(str2,200,fpin);
fscanf(fpin,"%lf",&d);
printf("%s,%s%lf\n",str1,str2,d);
fclose(fpin);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-05-02
文件编码有问题,保存为UTF-8格式试试
相似回答