#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file = fopen("employees.txt", "r+");
if (file == NULL) {
perror("Failed to open file");
return 1;
}
int id;
char name[100];
char department[100];
double salary;
// 临时文件用于存储修改后的内容
FILE *tempFile = fopen("temp.txt", "w");
if (tempFile == NULL) {
perror("Failed to create temp file");
fclose(file);
return 1;
}
while (fscanf(file, "%d,%99[^,],%99[^,],%lf\n", &id, name, department, &salary) == 4) {
salary *= 1.1; // 增加10%
fprintf(tempFile, "%08d,%s,%s,%.2lf\n", id, name, department, salary);
}
fclose(file);
fclose(tempFile);
// 删除原文件
if (remove("employees.txt") != 0) {
perror("Failed to remove original file");
return 1;
}
// 重命名临时文件为原文件名
if (rename("temp.txt", "employees.txt") != 0) {
perror("Failed to rename temp file");
return 1;
}
return 0;
}