C语言,为什么从文件中读取的内容后面有很多乱码?谁能帮忙解决?

//1.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"#include<std... // 1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <string> #include <iostream> using namespace std; struct conformation { char title[1024]; char content[1024]; char author[20]; char time[20]; }; int main(int argc, char* argv[]) { conformation file; /* int flg; system("mode con cols=60 lines=15&color 2f"); system("cls"); cout<<"\n\n\n\n\n"<<endl; cout<<"\t\t\t1.添加信息"<<endl; cout<<"\t\t\t2.删除信息"<<endl; cout<<"\t\t\t3.改变信息"<<endl; cout<<"\t\t\t4.查看信息"<<endl; cout<<"\t\t\t5.退出系统"<<endl; cout<<endl<<"\t\t\t请选择:"; cin>>flg; system("cls");*/ cout<<"请输入标题:"<<endl; cin>>file.title; cout<<"请输入内容:"<<endl; cin>>file.content; cout<<"请输入时间:"<<endl; cin>>file.time; cout<<"请输入作者:"<<endl; cin>>file.author; FILE *fp; char filename[20]; cout<<"请输入文件名称:"<<endl; cin>>filename; fp=fopen(filename,"wt"); if(fp==NULL) printf("Cannot not open the file!\n"); fprintf(fp,"标题:"); fprintf(fp,"%s",file.title); fprintf(fp,"\n"); fprintf(fp,"内容:"); fprintf(fp,"%s",file.content); fprintf(fp,"\n"); fprintf(fp,"时间:"); fprintf(fp,"%s",file.time); fprintf(fp,"\n"); fprintf(fp,"作者:"); fprintf(fp,"%s",file.author); fclose(fp); //system("cls"); conformation file1; cout<<"请输入文件名称:"<<endl; cin>>filename; fp=fopen(filename,"rt"); if(fp==NULL) printf("Cannot not open the file!\n"); char a[5][10]; fscanf(fp,"%s",&a[0]); fscanf(fp,"%s",&file1.title); fscanf(fp,"%s",&a[1]); fscanf(fp,"%s",&file1.content); fscanf(fp,"%s",&a[2]); fscanf(fp,"%s",&file1.time); fscanf(fp,"%s",&a[3]); fscanf(fp,"%s",&file1.author); fclose(fp); printf("%s",a[0]); printf("%s",file1.title); printf("%s",a[1]); printf("%s",file1.content); printf("%s",a[2]); printf("%s",file1.time); printf("%s",a[3]); printf("%s",file1.author); return 0; } 展开
 我来答
刑萌从冬梅
2020-05-06 · TA获得超过1224个赞
知道小有建树答主
回答量:1827
采纳率:100%
帮助的人:9.5万
展开全部
你不能既读到a中,又读到file1中,只能读到其中一个变量中.而且char
a[5][10]不够,要增大到与conformation相同,即char
a[5][1024].下面修改为只读到file1中:
//char
a[5][10];
//fscanf(fp,"%s",&a[0]);
fscanf(fp,"%s",&file1.title);
//fscanf(fp,"%s",&a[1]);
fscanf(fp,"%s",&file1.content);
//fscanf(fp,"%s",&a[2]);
fscanf(fp,"%s",&file1.time);
//fscanf(fp,"%s",&a[3]);
fscanf(fp,"%s",&file1.author);
fclose(fp);
//printf("%s",a[0]);
printf("%s\n",file1.title);//加'\n'
printf("%s",file1.title);
//printf("%s",a[1]);
printf("%s\n",file1.content);//printf("%s",file1.content);
//printf("%s",a[2]);
printf("%s\n",file1.time);//printf("%s",file1.time);
//printf("%s",a[3]);
printf("%s\n",file1.author);//printf("%s",file1.author);
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式