#include <stdio.h>
#include <string.h>
void main() {
FILE* fp;
char line[1024];
char filename[1024];
int length;
int counter=0;
while (1) {
// fopen(filename, "w");
int op = 0;
printf("1. new file");
scanf("%d", &op);
switch (op) {
case -1: break;
case 1:
printf("filename>>");
scanf("%s", filename);
length = strlen(filename);
printf("length = %ld \n", strlen(filename));
filename[length] = '.';
filename[length + 1] = 't';
filename[length + 2] = 'x';
filename[length + 3] = 't';
filename[length + 4] = '\0';
fp=fopen(filename, "w");
while (fgets(line, sizeof line, stdin) != NULL)
{
if (line[0] == 'q' || line[0] == 'Q')
{
printf("end of line \n");
fclose(fp);
break;
}
else
fputs(line, fp);
}
}
}
}
我制作了这段代码来制作我想要的文本文件。但问题是,当我运行这个程序时,它从第二行写入输入,而不是第一行。
为什么会发生这种情况?我想从第一行写输入。如何更改此代码以在第一行编写?