文件-fscanf

a091003040421 2012-01-16 05:38:55
fscanf(fp,"%d%s%d%d",&st[i].no,st[i].a,&st[i].p,&st[i].k);
这个语句能把文件里的一连窜数据赋给结构体吗? 文件里的信息是101 Zhao 95 88 103
...全文
234 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
面包大师 2012-01-16
  • 打赏
  • 举报
回复
fscanf(fp,"%d",&n);//你这儿都把101读出来了下边肯定出错啊。。。
while(n!=0)
{
fscanf(fp,"%d%s%d%d",&st[i].no,st[i].a,&st[i].p,&st[i].k);//上边把101都读出来了。。。
//fscanf(fp,"%d",&st[i].no);
//fscanf(fp,"%s",&st[i].a);
//fscanf(fp,"%d",&st[i].p);
//fscanf(fp,"%d",&st[i].k);
//fscanf(fp,"%d",&n);
i++;
}

修改:
fscanf(fp,"%d",&n);
while(n!=0)
{
st[i].no = n;
fscanf(fp,"%s%d%d"st[i].a,&st[i].p,&st[i].k);
//fscanf(fp,"%d",&st[i].no);
//fscanf(fp,"%s",&st[i].a);
//fscanf(fp,"%d",&st[i].p);
//fscanf(fp,"%d",&st[i].k);
fscanf(fp,"%d",&n);
i++;
}
a091003040421 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 czh3642210 的回复:]
C/C++ code


#include <stdio.h>
FILE *stream;
struct A
{
int no;
char a[32];
int p;
int k;
};
void main()
{
A st[1];
int i = 0;
stream = fopen( "data.txt", ……
[/Quote]
/*(50)以下程序从文件"student.txt"读取学生的学号、姓名、平时成绩和考试成绩,
再从键盘上输入一个成绩,将所有考试成绩达到或超过该成绩的学生数据写到新的文本文件"studentD.txt"。
文件的最后一行为0表示学生数据结束。
设文件student.txt的内容为
101 Zhao 95 58
103 Qian 75 81
105 Sun 99 91
107 Li 80 67
0
运行时键盘输入:80
则生成新文件studentD.txt的内容为:
103 Qian 75 81
105 Sun 99 91
0
例示说明:student.txt中考试成绩在80分以上的Qian与Sun信息写到studentD.txt
*/
#include<stdio.h>
struct student
{
int no;
char a[30];
int p;
int k;
}st[4];
main()
{
int i=0,j,n,d;
FILE *fp;
if((fp=fopen("e:\\student.txt","r"))==NULL)
printf("打开文件失败");
else
{ fscanf(fp,"%d",&n);
while(n!=0)
{
fscanf(fp,"%d%s%d%d",&st[i].no,st[i].a,&st[i].p,&st[i].k);
//fscanf(fp,"%d",&st[i].no);
//fscanf(fp,"%s",&st[i].a);
//fscanf(fp,"%d",&st[i].p);
//fscanf(fp,"%d",&st[i].k);
//fscanf(fp,"%d",&n);
i++;
}
}
fclose(fp);
fopen("e:\\studentD.txt","w");
printf("请输入划分的成绩:");
scanf("%d",&d);
j=i-1;
for(i=0;i<4;i++)
printf("*************************************************");
for(i=0;i<j;i++)
if(st[i].no!=0 && st[i].k>=d)
{
fprintf(fp,"%d",st[i].no);
fprintf(fp,"%s",st[i].a);
fprintf(fp,"%d",st[i].p);
fprintf(fp,"%d\n",st[i].k);
}
fclose(fp);
}
a091003040421 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 anyidan 的回复:]
fscanf(stream,"%d %s %d %d",&st[i].no,st[i].a,&st[i].p,&st[i].k);


加上空格
[/Quote]
加空格也不行
AnYidan 2012-01-16
  • 打赏
  • 举报
回复
fscanf(stream,"%d %s %d %d",&st[i].no,st[i].a,&st[i].p,&st[i].k);


加上空格
面包大师 2012-01-16
  • 打赏
  • 举报
回复

#include <stdio.h>
FILE *stream;
struct A
{
int no;
char a[32];
int p;
int k;
};
void main()
{
A st[1];
int i = 0;
stream = fopen( "data.txt", "r+" );
if( stream == NULL )
printf( "The file fscanf.out was not opened\n" );
else
{
/* Set pointer to beginning of file: */
fseek( stream, 0L, SEEK_SET );
/* Read data back from file: */
fscanf(stream,"%d%s%d%d",&st[i].no,st[i].a,&st[i].p,&st[i].k);
/* Output data read: */
fclose( stream );
}
}

你发下你全部的代码,或者试下我这个,我这个是好的啊
a091003040421 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 czh3642210 的回复:]
字符串也要加&
[/Quote]
如果是scanf 就不加 我运行时发现fscanf加不加都可以 但是问题不在这里好像
面包大师 2012-01-16
  • 打赏
  • 举报
回复
字符串也要加&
a091003040421 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lile1234_show 的回复:]
引用 2 楼 a091003040421 的回复:

引用 1 楼 czh3642210 的回复:
是的,从一个流中执行格式化输入到结构体

那为什么我输出后int型全部是0 字符串什么也没有呢?

第二个少了个&符。
[/Quote]
struct student
{
int no;
char a[30];
int p;
int k;
}st[4];
第二个是字符串
面包大师 2012-01-16
  • 打赏
  • 举报
回复
fscanf(fp,"%d%s%d%d",&st[i].no,st[i].a,&st[i].p,&st[i].k);//第二个少了个&
lee_鹿游原 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 a091003040421 的回复:]

引用 1 楼 czh3642210 的回复:
是的,从一个流中执行格式化输入到结构体

那为什么我输出后int型全部是0 字符串什么也没有呢?
[/Quote]
第二个少了个&符。
面包大师 2012-01-16
  • 打赏
  • 举报
回复
你分开试试,
fscanf(fp,"%d",&st[i].no);
fscanf(fp,"%s",&st[i].a);
fscanf(fp,"%d",&st[i].p);
fscanf(fp,"%d",&st[i].k);
a091003040421 2012-01-16
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 czh3642210 的回复:]
是的,从一个流中执行格式化输入到结构体
[/Quote]
那为什么我输出后int型全部是0 字符串什么也没有呢?
面包大师 2012-01-16
  • 打赏
  • 举报
回复
是的,从一个流中执行格式化输入到结构体

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧