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
140 views
in Technique[技术] by (71.8m points)

c - Modify an existing line in file and trim the rest of the line

Please help me, I made a function to modify a line in file using C language. Now my problem is that although the line is modified, the rest of the line I modified is still as is; For example if the new data I enter is shorter than the original line, my new data will be written, but the rest of the line will still be filled with old data. Please check the first line in the outputs below. Please advise me, thank you very much.

Original file:

Sam,Thomas,10-06-1995,26 Elhoreya Street,01234567899,[email protected]
Steven,Thomas,10-06-1995,26 Elhoreya Street,01234567899,[email protected]

Expected output file:

John,George,12-1-2000,23 MN Street,1234567899,[email protected]
Steven,Thomas,10-06-1995,26 Elhoreya Street,01234567899,[email protected]

Real output file:

John,George,12-1-2000,23 MN Street,1234567899,[email protected]@gmail.com
Steven,Thomas,10-06-1995,26 Elhoreya Street,01234567899,[email protected]

This is my function:

void modify(contact c[])
{
    int flag=0, k=0, count_matches=0, match_cont_pos[100], choose;
    char name[50], temp;

    FILE * file;
    file = fopen("info.txt", "rb+");

    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);

            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/65837494/modify-an-existing-line-in-file-and-trim-the-rest-of-the-line

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

1 Answer

0 votes
by (71.8m points)

Consider these suggestions, they are to lead you towards an answer:

  • Your file is like a byte array, modifying some bytes in the middle does not change the position of the remaining bytes in the file.
  • You may be overwriting part of one line and leaving extra characters from the old line in place if the new line is shorter than the old one.
    You might be overwriting part of the next line if the new line is longer than the old line
  • The code searches the 'C[]' array and not the file, is the array and file guaranteed to be in sync? Are you searching the array or the file?
  • The line " for(int i = 0; i < count ; i++)" has a variable 'Count'. Where is 'Count' defined?

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

...