Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

C program to modify an existing record in file

Please advise me, I am a beginner at c. I have created a function to modify contacts in a file, the problem is that the record I modify gets inserted as a new record at the end of the file, but I need to replace the existing record with the new one. The code does get an input from user and checks its existence in the file to modify it with the new data, I used fseek() so the pointer navigates to the needed position, but nothing changed. Thank you in advance.

void modify(contact c[])
{
    
    FILE *f;
    int flag=0;
    int count_matches=0, match_cont_pos[100], choose;
    int k=0;

    char  name[50];

    FILE * file;
    file = fopen("info.txt", "ab+");
    char temp;


    if(file == NULL)
    {
        printf("Error opening file
");
        exit(1);
    }

    else
    {
        printf("
Enter the name you want to search: ");
        scanf("%s",&name);

        for(int j = 0; name[j] != ''; j++)
            name[j] = tolower(name[j]);

        for(int i = 0; i < count ; i++) {

            for(int j = 0; c[i].last_name[j] != ''; j++)
                c[i].last_name[j] = tolower(c[i].last_name[j]);

            if(strcmp(c[i].last_name,name) == 0)
            {
                match_cont_pos[k]=i;
                k++;
            }

        }
        for(int d=0;d<k;d++)
            printf("match pos %d
",match_cont_pos[d]);

        for(int d=0;d<k;d++){
            printf("
%s
%s
%d %d %d
%s
%d
%s

", c[match_cont_pos[d]].last_name,c[match_cont_pos[d]].first_name,c[match_cont_pos[d]].DoB.day,c[match_cont_pos[d]].DoB.month,c[match_cont_pos[d]].DoB.year,c[match_cont_pos[d]].street_address,c[match_cont_pos[d]].phone_number,c[match_cont_pos[d]].email);
        }

        if(k>1){

            printf("Enter the number of the record you want to modify: ");
            scanf("%d",choose);

            printf("Enter last name: ");
            scanf("%s",&c[match_cont_pos[choose-1]].last_name);
            printf("Enter first name: ");
            scanf("%s",&c[match_cont_pos[choose-1]].first_name);
            printf("Enter phone number: ");
            scanf("%d",&c[match_cont_pos[choose-1]].phone_number);
            printf("Enter email: ");
            scanf("%s",&c[match_cont_pos[choose-1]].email);
            printf("Enter birth date: ");
            scanf("%d %d %d",&c[match_cont_pos[choose-1]].DoB.day,&c[match_cont_pos[choose-1]].DoB.month,&c[match_cont_pos[choose-1]].DoB.year);
            printf("Enter the address: ");
            scanf("%c",&temp); // temp statement to clear buffer
            scanf("%[^
]",&c[match_cont_pos[choose-1]].street_address);
            rewind(file);
            
            fseek(file,-sizeof(c),SEEK_SET);
            
            fprintf(file,"%s,%s,%d-%d-%d,%s,%d,%s",c[match_cont_pos[choose-1]].last_name,c[match_cont_pos[choose-1]].first_name,c[match_cont_pos[choose-1]].DoB.day,c[match_cont_pos[choose-1]].DoB.month,c[match_cont_pos[choose-1]].DoB.year,c[match_cont_pos[choose-1]].street_address,c[match_cont_pos[choose-1]].phone_number,c[match_cont_pos[choose-1]].email);
        }

    }
    fclose(file);
}
question from:https://stackoverflow.com/questions/65835694/c-program-to-modify-an-existing-record-in-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...