【发布时间】:2017-11-17 07:16:31
【问题描述】:
我正在尝试删除 file1 的第 20 行。该程序的其余部分是一个简单的外壳,它只能工作我无法记录用户使用的命令的历史。
当我尝试插入命令时出现的错误消息是:Segmentation fault (core dumped)
我正在执行的代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#define GetCurrentDir getcwd //get the path of file
#define BUFFER_LEN 1024
int ch;
FILE *file1, file2;
char line[BUFFER_LEN]
while(1){
if(!fgets(line, BUFFER_LEN, stdin))
{
break;
}
else if(line, BUFFER_LEN, SIGQUIT){
fopen("fileOne.c","r");
while((ch = getc(file1)) != EOF){
printf("%s",ch);
}
}
if(count<20)
{
fputs(argv[100] ,file1);
}
else{
fclose(file1);
file1 = fopen("fileOne.c","r");
rewind(file1);
file2 = fopen("repicla.c","w");
while((ch = getc(file1)) != EOF){
if((ch = getc(file1)) != "\n"){
count++;
if(count != 20){
putc(ch, file2);
}
}
}
fclose(file1);
fclose(file2);
remove("fileOne.c");
rename("replica.c","fileOne.c");
fputs(argv[100] ,file1);
}
}
【问题讨论】:
-
1)
fopen("fileOne.c","r");-->file1 = fopen("fileOne.c","r");,printf("%s",ch);-->printf("%c",ch);. BTW 这个可以编译吗? -
可以编译,还是报同样的错误