喜欢FreeBSD

在FreeBSD中记下的

  山阳博客 :: 联系 :: 聚合  :: 登录
  73 Posts :: 0 Stories :: 7 Comments :: 0 Trackbacks

留言簿(0)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

2008年7月19日 #

#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);
}

 

posted @ 2008-07-19 01:51 hifreebsd@tom.com的博客 阅读(121) | 评论 (0)编辑 收藏