hello, i previously posted here asking about print to screen issue, i got help and i thought it worked cuz i run it and it worked flawlessly and then now i add some stuff to it and now everything broken and when i revert it, its still broken so it seems like it wasnt fixed at all...
here's the problem, i add records to a text file like
name id gross net pay
john 102 2300 1980
and then now i use a diff function to locate a single record based on user input to change a certain record using fseek. the file pointer is giving me problems in overwriting the correct record. here's the code
And also i can make a record then load it and it will print the correct record on the screen but if i just open the program and try to edit a record that was previously made it will show garbage. i know this is due to the struct not containing the correct info, how can i fix this?Code:void edit_record(FILE *rptr) { int editchoice=1; FILE *writeptr; struct employee_data employee; if ((writeptr=fopen("Payroll.txt", "r+")) == NULL){ printf("file cannot be opened\n"); } while (editchoice== 1) { printf("Enter record number to be editted\n"); scanf("%d", &employee.num); //GETTING DATA FROM FILE TO PRINT TO SCREEN fseek(writeptr, (employee.num -1)*sizeof(struct employee_data), SEEK_CUR); fscanf(writeptr, "%d %s %s %d %f %f %f %f %f %f %f" , &employee.num, employee.first_name, employee.last_name, &employee.id_number, &employee.pay_rate, &employee.sthours, &employee.one_and_half_hour, &employee.double_hour, &employee.gross_pay, &employee.deductions, &employee.net_pay); printf("%-15s %-15s %-12s %-14s %-12s\r\n", "First name", "Last name", "Gross pay", "deductions", "net Pay"); printf("%-15s %-15s %-12.2f %-14.2f %-12.2f\n", employee.first_name, employee.last_name, &employee.gross_pay, &employee.deductions, &employee.net_pay); if (employee.num == 0){ printf("record number %d has no info\n", employee.num); }// end if //OBTAINING INFORMATION// printf("adding a new record, option 1\n"); printf("Enter first name\n"); scanf("%s", &employee.first_name); printf("Enter last name\n"); scanf("%s", &employee.last_name); printf("Enter Employee's ID number\n"); scanf("%d", &employee.id_number); printf("Enter Pay Rate\n"); scanf("%f", &employee.pay_rate); printf("Enter Straight time hours\n"); scanf("%f", &employee.sthours); printf("Enter One and Half time Hours\n"); scanf("%f", &employee.one_and_half_hour); printf("Enter double time hours\n"); scanf("%f", &employee.double_hour); //CALCULATIONS// employee.gross_pay = employee.pay_rate*employee.sthours + employee.pay_rate*1.5*employee.one_and_half_hour + employee.pay_rate*2*employee.double_hour; employee.nis_tax = employee.gross_pay * 0.025; employee.gross_not_nis = employee.gross_pay - employee.nis_tax; employee.nht_tax = employee.gross_pay * 0.02; employee.edu_tax = employee.gross_pay * 0.02; employee.heart_tax = employee.gross_pay * 0.03; employee.Income_tax = (employee.gross_pay-employee.nis_tax) - 8484; if (employee.gross_not_nis>= 8484){ employee.Income_tax= ((employee.gross_pay-employee.nis_tax - 8484)*0.25)+(employee.gross_pay-employee.nis_tax) - 8484; } //end if if (employee.gross_not_nis < 8484){ employee.Income_tax=0; } //end if employee.deductions=employee.nis_tax + employee.nht_tax + employee.edu_tax + employee.heart_tax + employee.Income_tax; employee.net_pay=employee.gross_pay - employee.deductions; employee.num =employee.num+1; //WRITING TO FILE// fseek(writeptr, (employee.num-1) * sizeof(struct employee_data), SEEK_CUR); fprintf(writeptr, "%-10d%-15s%-15s%-12.2f%-8d%-12.2f%-12.2f%-12.2f%-12.2f%-14.2f%-12.2f\r\n" , employee.num, employee.first_name, employee.last_name, employee.id_number, employee.pay_rate, employee.sthours, employee.one_and_half_hour, employee.double_hour, employee.gross_pay, employee.deductions, employee.net_pay); fclose(writeptr); printf("do you want to edit another file? press 1 to do so or 0 to exit"); scanf("%d", &editchoice); } //end while }//end fuction
when i do get it to work, it overwrites the first one correctly but as more records are made it starts overwritng wrong like start overwriting at the second column and leave the first untouched with the original data and continue deviating more as the records increase. I want to know how to fix this as well.
thanks in advance, i know i am asking alot outta you guyssorry....



LinkBack URL
About LinkBacks
sorry.... 



