写程序删除一个文件的第十行
时间: 2023-03-26 16:01:18 浏览: 118
可以使用以下代码删除一个文件的第十行:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp1, *fp2;
char filename[100], ch;
int delete_line, temp = 1;
printf("请输入文件名:");
scanf("%s", filename);
fp1 = fopen(filename, "r");
ch = getc(fp1);
while (ch != EOF) {
printf("%c", ch);
ch = getc(fp1);
}
rewind(fp1);
printf("\n请输入要删除的行数:");
scanf("%d", &delete_line);
fp2 = fopen("temp.txt", "w");
ch = getc(fp1);
while (ch != EOF) {
if (ch == '\n') {
temp++;
}
if (temp != delete_line) {
putc(ch, fp2);
}
ch = getc(fp1);
}
fclose(fp1);
fclose(fp2);
remove(filename);
rename("temp.txt", filename);
printf("\n第 %d 行已被删除。\n", delete_line);
return ;
}
阅读全文