50 likes | 166 Views
예제 Ex 2-5. 2000242009 김도영. #include <fcntl.h> #define TABSIZE 100 #define BUFSIZE 512 main(argc,argv) int argc; char *argv[]; { int n, i, entry, len; int fd, open(); long offset; static struct { long offset; int len;
E N D
예제 Ex 2-5 2000242009 김도영
#include <fcntl.h> #define TABSIZE 100 #define BUFSIZE 512 main(argc,argv) int argc; char *argv[]; { int n, i, entry, len; int fd, open(); long offset; static struct { long offset; int len; } table[TABSIZE]; char buf[BUFSIZE];
entry = 0; offset = 0; while((n = read(fd, buf, BUFSIZE)) > 0) { for(i=0; i < n; i++) { table[entry].len++; offset++; if(buf[i] == '\n') table[++entry].offset = offset; abcde’\n’ fgh’\n’ wxyz’\n’ long형 변수 table 구조체
for(;;) { printf("Enter line number: "); scanf("%d",&n); if(--n < 0) break; lseek(fd, table[n].offset, 0); if(read(fd, buf, table[n].len) <= 0) continue; buf[table[n].len] = '\0'; printf("%s", buf); } close(fd); exit(0);