这个文件函数读写怎么不对

ithiker 2010-07-19 05:23:14
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

struct student_score

{

char name[10];

int num;

int score[3];

} stu[8];

void main()

{

int i;

FILE *fp, *fp1;

if((fp=fopen("stu.txt","r"))==NULL)

{

printf("Can't open file\n");

exit(0);

}

fp1=fopen("result.txt","w");

for(i=1;i<8; i+=2)

{

fseek(fp,sizeof(struct student_score)+1,1); /*fp从当前位置向前移动一个student_score结构大小字节,“+1”是每条记录多一个换行符*/

fread(&stu[i],sizeof(struct student_score),1,fp);

fwrite(&stu[i],sizeof(struct student_score),1,fp1);

printf("%s\n",stu[i].name);

}

fclose(fp);

fclose(fp1);

}


其中,stu.txt数据文件中的数据为:
101 zhao 85 60 69
102 wang 85 87 61
103 zhang 88 77 65
104 mali 80 84 72
105 he 78 67 76
106 liqi 85 83 80
107 jia 95 67 82
108 shi 67 83 74



程序的输出结果文件result.txt的内容应该为:
102 wang 85 87 61
104 mali 80 84 72
106 liqi 85 83 80
108 shi 67 83 74

...全文
116 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
ithiker 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 war10811 的回复:]
C/C++ code

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

struct student_score
{
char name[10];
int num;
int score[3];
} stu[8];

void main()
{
int i, j;……
[/Quote]
fscanf(fp, "%*[^\n]%*c");

这个地方的*的作用是什么,发现去掉也可以
ithiker 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cattycat 的回复:]
你循环怎么i+=2,显然跳过了一个记录。
你还是用fscanf和fprintf比较好。

for(i=0;i<8;i++)
{
fscanf(fp,"%d %s %d %d %d\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
fprint……
[/Quote]
这个为什么不能运行?
selooloo 2010-07-19
  • 打赏
  • 举报
回复
因为你的文件不是二进制写的,所以还是别用fread,rwrie了
#include <stdlib.h>

#include <stdio.h>

#include <string.h>

struct student_score

{

char name[10];

int num;

int score[3];

} stu[8];

int main()

{

int i;

FILE *fp, *fp1;

if((fp=fopen("stu.txt","r"))==NULL)

{

printf("Can't open file\n");

exit(0);

}

fp1=fopen("result.txt","w");

for(i=1;i<8; i+=2)

{
char buf[128]={0};
fgets(buf,128,fp);

fgets(buf,128,fp);
fputs(buf,fp1);
printf("%s\n",stu[i].name);

}

fclose(fp);

fclose(fp1);

}
war10811 2010-07-19
  • 打赏
  • 举报
回复

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

struct student_score
{
char name[10];
int num;
int score[3];
} stu[8];

void main()
{
int i, j;

FILE *fp, *fp1;

if((fp=fopen("stu.txt","r"))==NULL)
{
printf("Can't open file\n");
exit(0);
}

fp1=fopen("result.txt","w");

for(i=1;i<8; i+=2)
{
fscanf(fp, "%*[^\n]%*c");

fscanf(fp, "%d", &stu[i].num);
fprintf(fp1, "%d ", stu[i].num);

fscanf(fp, "%s", stu[i].name);
fprintf(fp1, "%s ", stu[i].name);

for(j = 0; j < 3; j++)
{
fscanf(fp, "%d", &stu[i].score[j]);
fprintf(fp1, "%d ", stu[i].score[j]);
}

fscanf(fp, "%*[^\n]%*c");
fprintf(fp1, "\n");
}

fclose(fp);
fclose(fp1);
}
cattycat 2010-07-19
  • 打赏
  • 举报
回复
你循环怎么i+=2,显然跳过了一个记录。
你还是用fscanf和fprintf比较好。

for(i=0;i<8;i++)
{
fscanf(fp,"%d %s %d %d %d\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
fprintf(fp1,,"%d %s %d %d %d\n",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
}

就可以了。
艾莎云 2010-07-19
  • 打赏
  • 举报
回复
LZ您的程序到底要干嘛?按原样输出啊,那肯定要把+2改成+1
zhaolinger2 2010-07-19
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20031128/04/2503667.html

给你一个参考
ithiker 2010-07-19
  • 打赏
  • 举报
回复
帮忙调调啊,不要只说不练,谢了~
文件这块我不熟...
zhaolinger2 2010-07-19
  • 打赏
  • 举报
回复
补充说明:你的每一个student_score都是26个字节那么长(设在int = 4字节的环境下),而你的第一行却只有18个字节,偏移过量,呵呵。
zhaolinger2 2010-07-19
  • 打赏
  • 举报
回复
你的那个注释就告诉了你程序的问题所在。

你对每一行都认为是相同的长度,都以为是sizeof(struct student_score)那么长,但是你的文件里明显不是这样的,每一行的长度各不相同,所以偏移量怎么可能一样呢?

70,037

社区成员

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

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