程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c語言-C語言文件fwrite的問題

c語言-C語言文件fwrite的問題

編輯:編程綜合問答
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);}

最佳回答:


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved