我正在用 C 训练文件处理,我试图在fseek()写入和读取时更改文件的单行或位置,fread()无论fwrite()是否更改变量并再次写入整个文件,但显然附加模式和写入模式都不允许您正如我在下面的示例中尝试的那样执行此操作:
void main()
{
FILE *file;
char answer;
char text[7] = "text 1\n";
char text2[7] = "text 2\n";
file = fopen("fseek.txt", "w"); //creating 'source' file
fwrite(text, sizeof(char), sizeof(text), file);
fwrite(text, sizeof(char), sizeof(text), file);
fwrite(text, sizeof(char), sizeof(text), file);
fclose(file);
scanf("%c", &answer);
switch(answer)
{
case 'a':
//attempt to change single line with append mode
file = fopen("fseek.txt", "a");
fseek(file, 7, SEEK_SET); //7 characters offset is the second line of the file
fwrite(text2, sizeof(char), sizeof(text), …Run Code Online (Sandbox Code Playgroud)