Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 7152 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
405 | Develop | git 브런치 배우기 (링크) | hooni | 2013.07.09 | 20571 |
404 | System/OS | [mac] 컨텍스트(Context) 메뉴 "다음으로 열기" 내용 정리 | hooni | 2013.07.10 | 18798 |
403 | Etc | 영어의 12 시제 (The twelve tenses of English) | hooni | 2013.07.12 | 15700 |
402 | Develop | [ios] App States | hooni | 2013.07.22 | 13195 |
401 | Develop | [ios] Background 에서 네트워크 사용 | hooni | 2013.07.22 | 11510 |
400 | Develop | [ios] 소소한 팁 (Rect,Point,Path,URL 등) | hooni | 2013.08.08 | 29457 |
399 | Develop | [ios] libxml/tree.h file not found | hooni | 2013.08.08 | 18709 |
398 | Develop | [ios] UI컨트롤러 샘플코드 | hooni | 2013.08.08 | 15425 |
397 | Develop | [ios] 최신 UI 모음.. (나중에 정리할 것) | hooni | 2013.08.09 | 0 |
396 | Develop | 아이 훌레시 작업중 ㅋㅋ | hooni | 2013.08.09 | 0 |
395 | Etc | [web] 제로보드 XE 템플릿에서 if문에 대해서 알아봅시다 | hooni | 2013.08.16 | 19815 |
394 | Etc | 베지어 곡선 (Bezier curve) | hooni | 2013.08.18 | 221431 |