Goal:我在Visual Studio2022中使用C语言将位于C:/Users/13383/Desktop/storage.txt的文件中"STORAGE_"之后的序列号减少7252.storage.txt中的数据如下图所示:
...
"STORAGE_7253":
...
"STORAGE_7254":
...
"STORAGE_7255":
...
Already done:我的节目如下所示:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1000
int main() {
FILE* fp;
char line[MAX_LINE_LENGTH];
char* pos;
int num;
if (fopen_s(&fp, "C:/Users/13383/Desktop/storage.txt", "r+") != 0) {
printf("Error opening file\n");
exit(1);
}
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
pos = strstr(line, "STORAGE_");
if (pos != NULL) {
num = atoi(pos + strlen("STORAGE_"));
printf("%d\n", num);
num -= 7252;
sprintf_s(pos + strlen("STORAGE_"), MAX_LINE_LENGTH - (pos - line) - strlen("STORAGE_"), "%d\"", num);
puts(line);
}
//fputs(line, fp);
}
fclose(fp);
printf("Done!\n");
return 0;
}
Problem:当我注释掉fputs(line, fp);
时,我发现变量"line"的内容是正确的:
但是当我取消注释"fputs"以在document中写入字符串时,它提示一个错误:
所以我想知道问题出在哪里,以及如何解决它?