C语言问题,文件中的那个的内容换行了之后怎么读?
#include<stdio.h>#include<stdlib.h>intmain(){FILE*fpin,*fpout;doublef=1,d;charstr[]="...
#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;
}
为什么总读出来乱码?
下面是对应的文件! 展开
#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;
}
为什么总读出来乱码?
下面是对应的文件! 展开
2个回答
展开全部
//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;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询