Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 7166 댓글 0
#define MAXSIZE 65535
#define CRLF "\n"
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
const char *getline(int sockfd){
static char buf[MAXSIZE + 1] = "";
static char *pbuf = buf;
int i;
ssize_t len;
char *retptr;
char *crlf;
do{
/* 개행문자 찾으면 */
if(crlf = strstr(pbuf, CRLF)){
retptr = pbuf;
pbuf = crlf + strlen(CRLF);
*crlf = '\0';
return retptr;
}
/* 못 찾았으면 */
len = strlen(pbuf);
for(i = 0; i < len; i++) buf[i] = pbuf[i];
pbuf = buf;
}while(read(sockfd, buf + len, MAXSIZE - len) > 0);
/* 데이터 읽고 다시 개행문자 있는지 검사하러 올라가자 */
return NULL; // 에러 또는 EOF
}
int main(){
const char *line;
int fd;
fd = open("test.c", O_RDONLY);
while(line = getline(fd))
printf("LINE : '%s'\n", line);
close(fd);
return 0;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
625 | Develop | [js] jQuery 코드 작성시 편리한 HTML 템플릿 | hooni | 2013.12.17 | 33120 |
624 | Develop | [js] jQuery 치트 시트 | hooni | 2013.12.18 | 36304 |
623 | Develop | [js] jQuery 충돌 회피 | hooni | 2013.12.17 | 38377 |
622 | Develop | [js] jQuery 셀랙터(selector) 요약 | hooni | 2013.12.17 | 9440 |
621 | Develop | [js] jQuery 셀 병합 1 | hooni | 2014.09.23 | 3567 |
620 | Develop | [js] jQuery 관련 사이트 링크 모음 | hooni | 2013.04.23 | 18280 |
619 | Develop | [js] jQuery 관련 문서 ㅎㅎ | hooni | 2013.04.23 | 31257 |
618 | Develop | [js] jQuery plugin 요약 | hooni | 2013.12.20 | 10753 |
617 | Develop | [js] jQuery 배열 루프(each) | hooni | 2013.12.17 | 10025 |
616 | Develop | [js] jQjuery $ 활용 | hooni | 2013.12.17 | 8907 |
615 | Develop | [js] Javascript로 만든 포트리스 (2010) 5 | hooni | 2017.03.03 | 2829 |
614 | Develop | [js] IE에서 인쇄 설정 팁 | hooni | 2013.04.23 | 10923 |