-4

try to use fseek to modify the name but it cannot return what result i want which is the name cannot be modify and remain the same

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");
}
unwind
  • 391,730
  • 64
  • 469
  • 606
BEAR
  • 1

1 Answers1

2

You open the file only for reading. You have to open the file in read-write mode if you want to both read and write.

fopen("name.txt","r+b"); // open read+write
Joni
  • 108,737
  • 14
  • 143
  • 193