c语言中创建文件时出现这是怎么回事啊???大哥大姐来帮忙!!!

微凉记忆 2011-11-27 07:27:33
#include<stdio.h>
struct st
{

char name[20];
long num1;
long math;
long totle;
}student;
FILE *fp;
int i;
if((fp=fopen("test.txt","wb+"))==NULL)
printf("wrong!!!");
for (i=0;i<10;i++)
{
scanf("%s%l%l",&student.name,&student.num1,&student.math);
student.totle=student.num1+student.math;
fseek(fp,i*sizeof(st),0);
if((fwrite(&student,sizeof(student),1,fp)!=1) printf("error!!!");
fclose(fp);
}
编译后产生fp.c:12:1: error: expected unqualified-id before ‘if’
fp.c:14:1: error: expected unqualified-id before ‘for’
fp.c:14:10: error: ‘i’ does not name a type
fp.c:14:15: error: ‘i’ does not name a type

这是怎么回事啊????

...全文
197 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
微凉记忆 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 duke56 的回复:]

引用 5 楼 weiliangjiyi 的回复:

修改了以后,写出的文件出现乱码!!!!为什么啊???
引用 2 楼 duke56 的回复:

C/C++ code
#include <stdio.h>

struct st
{

char name[20];
long num1;
long math;
long totle;
}student;

int m……
[/Quote]
但是改了之后写出的东西只显示一点点,后面的都没有显示啊????
duke56 2011-11-30
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 weiliangjiyi 的回复:]

引用 6 楼 duke56 的回复:

引用 5 楼 weiliangjiyi 的回复:

修改了以后,写出的文件出现乱码!!!!为什么啊???
引用 2 楼 duke56 的回复:

C/C++ code
#include <stdio.h>

struct st
{

char name[20];
long num1;
long math;
long totl……
[/Quote]

//原因是在于你定义的结构体
struct st
{
char name[20];//这里你是%s写入文件的,是按字符的
long num1;//而下边你是按%l写入的并非是ASCII写入文件的...
long math;
long totle;
}student;

duke56 2011-11-29
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 weiliangjiyi 的回复:]

修改了以后,写出的文件出现乱码!!!!为什么啊???
引用 2 楼 duke56 的回复:

C/C++ code
#include <stdio.h>

struct st
{

char name[20];
long num1;
long math;
long totle;
}student;

int main()
{
FILE *fp;
int i;
……
[/Quote]
wb+//用的是二进制写的的呀

微凉记忆 2011-11-27
  • 打赏
  • 举报
回复
修改了以后,写出的文件出现乱码!!!!为什么啊???
[Quote=引用 2 楼 duke56 的回复:]

C/C++ code
#include <stdio.h>

struct st
{

char name[20];
long num1;
long math;
long totle;
}student;

int main()
{
FILE *fp;
int i;
if((fp=fopen("test.txt","wb+"))=……
[/Quote]
微凉记忆 2011-11-27
  • 打赏
  • 举报
回复
我现在已经修改好了,但是创建的文件都是乱码,而且写出创建失败!!!!
modicum_ck 2011-11-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 duke56 的回复:]
C/C++ code
#include <stdio.h>

struct st
{

char name[20];
long num1;
long math;
long totle;
}student;

int main()
{
FILE *fp;
int i;
if((fp=fopen("test……
[/Quote]

++
duke56 2011-11-27
  • 打赏
  • 举报
回复
#include <stdio.h>

struct st
{

char name[20];
long num1;
long math;
long totle;
}student;

int main()
{
FILE *fp;
int i;
if((fp=fopen("test.txt","wb+"))==NULL)
printf("wrong!!!");
for (i=0;i<10;i++)
{
scanf("%s%l%l",&student.name,&student.num1,&student.math);
student.totle=student.num1+student.math;
fseek(fp,i*sizeof(st),0);
//下边多了个(
if(fwrite(&student,sizeof(student),1,fp)!=1) printf("error!!!");
}
fclose(fp);//放在循环里就关了文件了
return 0;
}
//稍改动了下
無_1024 2011-11-27
  • 打赏
  • 举报
回复

#include<stdio.h>
struct st
{

char name[20];
long num1;
long math;
long totle;
}student;
int main()
{
FILE *fp;
int i;
if((fp=fopen("test.txt","wb+"))==NULL)
printf("wrong!!!");
for (i=0;i<10;i++)
{
scanf("%s%ld%ld",&student.name,&student.num1,&student.math);
student.totle=student.num1+student.math;
fseek(fp,i*sizeof(st),0);
if(fwrite(&student,sizeof(student),1,fp) != 1)
printf("error!!!");
fclose(fp);
}
return 0;
}

69,375

社区成员

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

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