文件的有关问题
文件的问题
#include <stdio.h>
#include <string.h>
#define N 5
struct student
{
int xuehao;
char mingzi[20];
float stb;
float c;
}stu[N+1],stu1;
FILE *fp;
void main()
{
void ave(struct student *ps);
struct student *ps;
int i,k;
char check;
fp=fopen( "student.txt ", "wb ");
ps=stu;
for(i=0;i <N;i++,ps++)
{
scanf( "%d %s %f %f ",&ps-> xuehao,ps-> mingzi,&ps-> stb,&ps-> c);
}
ps=stu;
for(k=0;k <N;k++,ps++)
{
printf( "%d %s %.2f %.2f\n ",ps-> xuehao,ps-> mingzi,ps-> stb,ps-> c);
}
ave(stu);
fflush(stdin);
check=getchar();
if(check== 'Y ' || check== 'y ')
{
fread(&stu[N+1],sizeof(stu[N+1]),1,fp);
fclose(fp);
printf( "读取文件成功\n ");
}
void ave(struct student *ps)
{
char check;
int i,j,k;
printf( "输入要删除的学号: ");
scanf( "%d ",&stu1.xuehao);
check=getchar();
if(check== 'Y ' || check== 'y ')
{
fwrite(&stu[N+1],sizeof(stu[N+1]),1,fp);
printf( "写入文件成功\n ");
}
for(i=0;i <N;i++,ps++)
{
if(ps-> xuehao==stu1.xuehao)
break;
}
for(j=i;j <N;j++)
{
stu[j].xuehao=stu[j+1].xuehao;
strcpy(stu[j].mingzi,stu[j+1].mingzi);
stu[j].stb=stu[j+1].stb;
stu[j].c=stu[j+1].c;
}
ps=stu;
for(k=0;k <N-1;k++,ps++)
{
if(i> k)
printf( "%d ",ps-> xuehao);
else if (i <=k)
printf( "%d ",--ps-> xuehao);
printf( " %s %.2f %.2f\n ",ps-> mingzi,ps-> stb,ps-> c);
}
}
****************************************************************
: error C2143: syntax error : missing '; ' before 'type '
: error C2143: syntax error : missing '; ' before 'type '
error C2065: 'j ' : undeclared identifier
帮小弟看看~~如何进行文件的运用。
------解决方案--------------------
这样就可以了 函数不可以嵌套定义,ave()需要在外面定义。
#include <stdio.h>
#include <string.h>
#define N 5
struct student
{
int xuehao;
char mingzi[20];
float stb;
float c;
}stu[N+1],stu1;
FILE *fp;
void ave(struct student *ps)
{
char check;
int i,j,k;
printf( "输入要删除的学号: ");
scanf( "%d ",&stu1.xuehao);
check=getchar();
if(check== 'Y ' || check== 'y ')
{
fwrite(&stu[N+1],sizeof(stu[N+1]),1,fp);
printf( "写入文件成功\n ");
}
for(i=0;i <N;i++,ps++)
{
if(ps-> xuehao==stu1.xuehao)
break;
}
for(j=i;j <N;j++)
{
stu[j].xuehao=stu[j+1].xuehao;
strcpy(stu[j].mingzi,stu[j+1].mingzi);
stu[j].stb=stu[j+1].stb;
stu[j].c=stu[j+1].c;
}
ps=stu;
for(k=0;k <N-1;k++,ps++)
{
if(i> k)
printf( "%d ",ps-> xuehao);
else if (i <=k)
#include <stdio.h>
#include <string.h>
#define N 5
struct student
{
int xuehao;
char mingzi[20];
float stb;
float c;
}stu[N+1],stu1;
FILE *fp;
void main()
{
void ave(struct student *ps);
struct student *ps;
int i,k;
char check;
fp=fopen( "student.txt ", "wb ");
ps=stu;
for(i=0;i <N;i++,ps++)
{
scanf( "%d %s %f %f ",&ps-> xuehao,ps-> mingzi,&ps-> stb,&ps-> c);
}
ps=stu;
for(k=0;k <N;k++,ps++)
{
printf( "%d %s %.2f %.2f\n ",ps-> xuehao,ps-> mingzi,ps-> stb,ps-> c);
}
ave(stu);
fflush(stdin);
check=getchar();
if(check== 'Y ' || check== 'y ')
{
fread(&stu[N+1],sizeof(stu[N+1]),1,fp);
fclose(fp);
printf( "读取文件成功\n ");
}
void ave(struct student *ps)
{
char check;
int i,j,k;
printf( "输入要删除的学号: ");
scanf( "%d ",&stu1.xuehao);
check=getchar();
if(check== 'Y ' || check== 'y ')
{
fwrite(&stu[N+1],sizeof(stu[N+1]),1,fp);
printf( "写入文件成功\n ");
}
for(i=0;i <N;i++,ps++)
{
if(ps-> xuehao==stu1.xuehao)
break;
}
for(j=i;j <N;j++)
{
stu[j].xuehao=stu[j+1].xuehao;
strcpy(stu[j].mingzi,stu[j+1].mingzi);
stu[j].stb=stu[j+1].stb;
stu[j].c=stu[j+1].c;
}
ps=stu;
for(k=0;k <N-1;k++,ps++)
{
if(i> k)
printf( "%d ",ps-> xuehao);
else if (i <=k)
printf( "%d ",--ps-> xuehao);
printf( " %s %.2f %.2f\n ",ps-> mingzi,ps-> stb,ps-> c);
}
}
****************************************************************
: error C2143: syntax error : missing '; ' before 'type '
: error C2143: syntax error : missing '; ' before 'type '
error C2065: 'j ' : undeclared identifier
帮小弟看看~~如何进行文件的运用。
------解决方案--------------------
这样就可以了 函数不可以嵌套定义,ave()需要在外面定义。
#include <stdio.h>
#include <string.h>
#define N 5
struct student
{
int xuehao;
char mingzi[20];
float stb;
float c;
}stu[N+1],stu1;
FILE *fp;
void ave(struct student *ps)
{
char check;
int i,j,k;
printf( "输入要删除的学号: ");
scanf( "%d ",&stu1.xuehao);
check=getchar();
if(check== 'Y ' || check== 'y ')
{
fwrite(&stu[N+1],sizeof(stu[N+1]),1,fp);
printf( "写入文件成功\n ");
}
for(i=0;i <N;i++,ps++)
{
if(ps-> xuehao==stu1.xuehao)
break;
}
for(j=i;j <N;j++)
{
stu[j].xuehao=stu[j+1].xuehao;
strcpy(stu[j].mingzi,stu[j+1].mingzi);
stu[j].stb=stu[j+1].stb;
stu[j].c=stu[j+1].c;
}
ps=stu;
for(k=0;k <N-1;k++,ps++)
{
if(i> k)
printf( "%d ",ps-> xuehao);
else if (i <=k)