[ create a new paste ] login | about

Link: http://codepad.org/U8HzseFI    [ raw code | output | fork ]

C, pasted on Nov 19:
// ./a.out <出力ファイル名> <出力開始行> <出力終了行>
//ex) ./a.out test.txt 2 4
//上のはtest.txt.の2~4行目を出力という意味

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>

int main (int argc, const char * argv[]) {
    char buf[512];
    int fp, n, linenum, lf;

    lf = 0;
    linenum = atoi(argv[2]);

    //見やすくするため
    printf("\n\n");

    fp = open(argv[1], O_RDONLY);
    
    while ((n = (int)read(fp, buf, 1)) > 0) {
        
        if (buf[0] == '\n' || buf[0] == '\r') {
            lf = 1;
        }else if (lf == 1) {
            if (linenum - 1 == atoi(argv[3])) break; //出力終了行の判定
            printf("%4d  ", linenum); fflush(stdout);
            linenum++;
            lf = 0;
       }
        
        if (atoi(argv[2]) <= linenum) {
            write(1, buf, n);
        }

    } //end while
    
    close(fp);

    return 0;
}


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: