C语言文件fwrite的问题

我用fwrite对文件进行更新时,却将我想更新行数之前的全都清空了,改怎么写才对啊
比如想更新第二行,却将第一行重写了
#include
#include
#include

typedef struct{
int number;
char name[20];
int age;
char gender[5];
} Student;
char filename[20];

int main(void){
FILE * fp=NULL;
void creatFile(FILE * fp);
void insertStudent();
void queryStudent();
void updateStudent();
void deleteStudent();
void displayAll();
void quit();
int choose =1;
while (choose){
printf(" *****************please choose function******************* \n");
printf(" 1:creatfile 2:insert studnet information \n");
printf(" 3:query Studnet information 4:update studnet information \n");
printf(" 5:delete information 6: display file ** \n");
printf(" **
******************** 0:exit *****************************\n");
printf(" ******please choose the function******\n ");
scanf ("%d",&choose );
switch(choose){
case 1:creatFile(fp);break ;
case 2:insertStudent();break;
case 3:queryStudent();break;
case 4:updateStudent();break;
case 5:deleteStudent();break;
case 6:displayAll();break;
case 0:quit();break;
}
printf(" ******please press 1 to return menu*****\n ");
scanf ("%d",&choose );
}
return 0;
}

void creatFile(FILE * fp){
printf(" *****please enter filename*****\n ");
scanf("%s",filename);
if((fp=fopen(filename,"ab+"))==NULL)
{
printf("can't open file\n");
getchar();
exit(0);
}
else{
printf(" open file success\n");
}
}

void insertStudent(){
FILE * fp;
if((fp=fopen(filename,"ab"))==NULL) {
printf(" file open failure\n");
}
else{
int flag=1;
while(flag){
Student stu;
printf(" ****please enter student information****\n");
printf(" enter student number:\n");
scanf("%d",&stu.number);
printf(" enter student name:\n");
scanf("%s",stu.name);
printf(" enter student age:\n");
scanf("%d",&stu.age);
printf(" enter student gender:\n");
scanf("%s",stu.gender);
fwrite(&stu,sizeof(Student),1,fp);
printf(" continue? 1(yes) or 0(no) \n");
scanf("%d",&flag);
}
fclose(fp);
}
}

void queryStudent(){

FILE * fp;
Student stu;
int number;
fp=fopen(filename,"rb");
printf(" please enter the number which you want to query\n");
scanf("%d",&number);
fseek(fp,number*sizeof(Student),0);
fread(&stu,sizeof(Student),1,fp);
printf(" number name age gender\n");
printf("%10d%10s%10d%10s\n",stu.number,stu.name,stu.age,stu.gender);
fclose(fp);
}

void updateStudent(){
FILE* fp;
Student stu;
if((fp=fopen(filename,"wb"))==NULL){
printf("can't find the file\n");
exit(0);
}
else{
int number;
printf(" please enter the number which you want to update\n");
scanf("%d",&number);
fseek(fp,number*sizeof(Student),0);
printf(" enter student number:\n");
scanf("%d",&stu.number);
printf(" enter student name:\n");
scanf("%s",stu.name);
printf(" enter student age:\n");
scanf("%d",&stu.age);
printf(" enter student gender:\n");
scanf("%s",stu.gender);
fwrite(&stu,sizeof(Student),1,fp);
printf("update success\n");
fclose(fp);
}
}

void deleteStudent(){

FILE* fp;
Student stu;
if((fp=fopen(filename,"wb"))==NULL){
printf("can't find the file\n");
exit(0);
}
else{
int number;
printf(" please enter the number which you want to delete\n");
scanf("%d",&number);
fseek(fp,number*sizeof(Student),0);
stu.age=stu.number=NULL;
strcpy(stu.name,"");
strcpy(stu.gender,"");
fwrite(&stu,sizeof(Student),1,fp);
printf("delete success\n");
fclose(fp);

}
}

void displayAll(){
FILE* fp;
Student stu;
if((fp=fopen(filename,"rb"))==NULL){
printf("can't find the file\n");
exit(0);
}
else{
printf(" number name age gender\n");
while(!feof(fp)){
fread(&stu,sizeof(Student),1,fp);
printf("%10d%10s%10d%10s\n",stu.number,stu.name,stu.age,stu.gender);
}
fclose(fp);
}
}

void quit(){
printf("exit************\n");
exit(0);}

6个回答

需要添加内容可以用w+或者a的方式打开文件

操作文件的标志不对

void updateStudent(){
FILE* fp;
Student stu;
        if((fp=fopen(filename,"wb"))==NULL){     //  此处不能用 w ,它会销毁文件之前的内容

wb方式为写方式,若文件不存在则创建,文件存在则删除原文件重新创建新文件,wb+方式才是读写方式,打开且可更改

在updateStudent函数中,你使用wb打开文件的。wb是写模式,二进制模式。因为写模式,所以一开始就删除了。你试一下用ab来打开文件

改成ab也不行 它会接着文件末尾添加,定位没用,更新不了

不能更新吧,只能重写

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问