#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#define INPUT_FILE "/home/chen/temp/temp/input.txt"
int main()
{
FILE *in;
char buffer[1024];
pid_t pid;
int delete_num; // 欲删除的行数
char fro_to[20];
int status;
unsigned long lines = 0;
in = fopen(INPUT_FILE, "r");
if (NULL == in)
{
printf("fopen error!\n");
}
while(NULL != fgets(buffer, sizeof(buffer), in))
{
lines++;
}
// 输出文件的总行数
printf("The file \"%s\" has %ld lines.\n", INPUT_FILE, lines);
if (0 == lines) return 0;
printf("You want to delete fomr 1 to __:");
scanf("%d", &delete_num);
sprintf(fro_to, "1, %dd", delete_num);
while (delete_num > 0 && delete_num <lines + 1 )
{
pid = fork();
if (-1 == pid)
{
perror("fork failed!\n");
return(1);
}
else if (0 == pid)
{ /* pid为0,子进程 */
execlp("sed", "sed", "-i", fro_to, "input.txt",(char *)0);
perror("execl failed!\n");
_exit(2);
}
else
{
/* pid大于0,父进程 */
break;
}
}
wait(&status);
printf("ok, successfull!\n");
return(0);
}