尝试使用fseek修改名称 但它无法返回我想要的结果,这个名称无法修改并保持不变
struct phonebook { char name[20]; };
struct phonebook a;
char temp[20];
cpPtr=fopen("name.txt","rb");//open the file
while(fread(&a,sizeof(a),1,cpPtr)==1){
printf("Please enter name :\n");//require user to enter name
scanf("%s",&temp);//temporary variable
fflush(stdin);
if(stricmp(a.name,temp)==0){
printf("NAME :%s\n",a.name);
else
printf("The name is not exist");
getch();
}
printf("Please enter new NAME :");
scanf("&s",a.name);
fflush(stdin);
fseek(cpPtr,-sizeof(a),SEEK_CUR);//is there any wrong with seek?
fwrite(&a,sizeof(a),1,cpPtr);
fclose(cpPtr);
printf("Name is modified");
getch();
system("cls");
}
答案 0 :(得分:2)
您只能打开文件进行阅读。如果要同时读写,则必须以读写模式打开文件。
fopen("name.txt","r+b"); // open read+write