问题:自定义函数在单独使用的时候可以正常使用,但是实际使用会在没有输入任何字符直接跳过。。
void Deletebook() {
FILE* fileptr1, * fileptr2; //读和写指针
char fileName[30], buffer[10]; // 保存文件名
char ch; //在文件移动读字符
int delete_line, flag, temp = 1;
fileptr1 = fopen("librecord.txt", "r");
if (fileptr1 == NULL)
{
printf("Error: Could not find file.\n");
exit(1);
}
// 开头
ch = getc(fileptr1);
while (ch != EOF)
{
printf("%c", ch);
//将单个字符移动到列表中、打印
ch = getc(fileptr1);
}
//指回开头
rewind(fileptr1);
printf("\n输入你想删除的图书所在的行\n:");
// scanf("%d", &buffer);
fgets(buffer, sizeof(buffer), stdin);
flag = sscanf(buffer, "%d", &delete_line);
if (flag < 1)//输入小于1行
{
printf("\nError!\n");
exit(1);
}
//用w打开文件
fileptr2 = fopen("tempFile.txt", "w");
ch = getc(fileptr1);
while (ch != EOF)
{
//数行数
if (ch == '\n')
++temp;
// 限制所输入的只能为数字选项
if (temp != delete_line)
{
putc(ch, fileptr2);
}
// 获取下个字符
ch = getc(fileptr1);
}
//关闭文件
fclose(fileptr1);
fclose(fileptr2);
remove("librecord.txt");// 删除所指文件
// 重命名
rename("tempFile.txt", "librecord.txt");
printf("\n The contents of file after being modified are as follows:\n");
fileptr1 = fopen("librecord.txt", "r");
ch = getc(fileptr1);
// 替换新数据入文件
while (ch != EOF)
{
printf("%c", ch);
ch = getc(fileptr1);
}
// 关闭文件 fileptr1
fclose(fileptr1);
}
这是有问题的↓