求:c语言文件读取问题,求告知哪里有误,读取的时候不完整,而且有重复。。
答案:2 悬赏:20
解决时间 2021-02-20 01:07
- 提问者网友:践踏俘获
- 2021-02-19 01:36
# include "stdio.h"
# include "stdlib.h"
# define M 20
struct book
{
long bh;
char mc[100];
char zz[100];
int jq; int sl;
}b[M];
void main()
{ int i;
FILE *fp;
fp=fopen("d:\\book.txt","r");
if(fp==NULL)
{
printf("Error!\n");
exit(0);
}
for(i=0;i {
fread(&b[i],sizeof(struct book),1,fp);
printf("%ld\n%s\n%s\n%d\n%d\n",b[i].bh ,b[i].mc ,b[i].zz ,b[i].jq ,b[i].sl );
break;
}
fclose(fp);
}
最佳答案
- 二级知识专家网友:不傲怎称霸
- 2021-02-19 01:56
# include "stdio.h"
# include "stdlib.h"
# define M 10
struct book
{
long bh;
char mc[100];
char zz[100];
int jq;
int sl;
}b[M];
void main()
{
int i;
FILE *fp;
fp=fopen("d:\\book.txt","r");
if(fp==NULL)
{
printf("Error!\n");
exit(0);
}
for(i=0;i
{
fscanf(fp,"%ld%s%s%d%d",&b[i].bh,b[i].mc,b[i].zz,&b[i].jq,&b[i].sl);//这样读
printf("%ld\n%s\n%s\n%d\n%d\n",b[i].bh ,b[i].mc ,b[i].zz ,b[i].jq ,b[i].sl );
//去掉break
}
fclose(fp);
}
全部回答
- 1楼网友:一池湖水
- 2021-02-19 02:55
#include
int main()
{
char buffer[1024]="423\t0.0\t0.0\t";
char a[10], b[10], c[10];
int r = sscanf(buffer, "%[^\t]\t%[^\t]\t%[^\t]", a, b, c);
(r == 3) && printf("a = %s, b = %s, c = %s\n", a, b, c);
return 0;
}
我要举报
大家都在看
推荐资讯