0

I was trying to create a function that receives a file, reads a line, and update some of the data.

so basically, I want to:

  1. Read the line and insert the data into local variables
  2. Move backward to the beginning of the salary item (in the file).
  3. Overwrite the new updated salary.

To bo me specific, the file includes employee's data (code, name, and salary) and the function needs to update the salary of each employee by any given addition.

example: 123, Joe, 1200

 void updateSalary(char* filename)
 {
    employee temp;
    char c;
    float increase;
    FILE *fup = fopen(filename, "r+");
    while ((c=fgetc(fup)) != EOF)
    {
    fscanf(fup, "%*d%s%f", &temp.name, &temp.salary);
        printf("How big is the increase to %s's salary?\n", temp.name);
        scanf("%f", &increase);
        while ((c=fgetc(fup)) != ' ')
            fseek(fup, -1, SEEK_CUR);
        fprintf(fup, "%f", temp.salary + increase);
        fseek(fup, 1, SEEK_CUR);
    }
 fclose(fup);
}

How I thought to overwrite the new salary: I used a while loop to look for the backspace that appeared before the salary data while moving backward every byte and look for it. Not working correctly. second, after the print of the new data, the file position is set for the next char ('\n') and I would like to skip this one, so I used fseek with 1 byte. didn't work either.

Thanks!

Ofek Pintok
  • 613
  • 1
  • 6
  • 13
  • If you move back 1 byte and read one byte, you will not move very far.... – Gerhardh May 18 '18 at 14:50
  • @Gerhardh I also used to move backward 4 bytes (as the size of float) but it didn't work either... – Ofek Pintok May 18 '18 at 14:51
  • [`fgetc` returns an `int`](https://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc)!! – Antti Haapala -- Слава Україні May 18 '18 at 14:51
  • 2
    Be aware that you cannot overwrite an existing line in a file if the length of the new line is different from the length of the line to be overwritten. – Jabberwocky May 18 '18 at 14:53
  • 1
    Also, **no sane program** updates text files in place like this. You might consider using `sqlite` - it is eventually easier than trying to avoid all pitfalls with text files! – Antti Haapala -- Слава Україні May 18 '18 at 14:53
  • 1
    ...or use fixed size records – Jabberwocky May 18 '18 at 14:53
  • well for one thing, please don't use `float`s for monetary units. (Just try what happens when someone receives `$ 1221.52`...) – Antti Haapala -- Слава Україні May 18 '18 at 14:54
  • 1
    ...and `while ((c=fgetc(fup)) != ' ') fseek(fup, -1, SEEK_CUR);` is weird for two reasons: 1. the file pointer doesn't move at all (-1 + 1 = 0), 2. you're trying to move back until you encounter a space, but lines are not separated by spaces but by `\n`. Your whole approach is totally wrong. – Jabberwocky May 18 '18 at 14:59
  • 1
    Rather than trying to update the file you are reading from it would be easier as well as safer to create a new file that is a copy of the old file with the changes you have made to the salary. This allows salary changes to not affect surrounding text in cases where the new salary is different in magnitude from the old salary. This is the approach with may of the old payroll programs with tape storage back before database systems were around. – Richard Chambers May 18 '18 at 15:05

0 Answers0