Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 7158 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
361 | Develop | [ios] iOS 4.0 beta 에서 3.1.3으로 다운그레이드 하는 법 | hooni | 2013.04.23 | 37530 |
360 | Develop | [c] 메시지큐(Message Queue) 설명.. (joinc) | hooni | 2013.04.23 | 14290 |
359 | Develop | [android] 단일 Thread 환경의 안드로이드에서 Handler를 사용 | hooni | 2013.04.23 | 26551 |
358 | Develop | [android] 점심 해결 앱 소스 코드 ㅋㅋ | hooni | 2013.04.23 | 76455 |
357 | Develop | [ios] 한샘 카달로그 앱 소스 (아이폰용) | hooni | 2013.04.23 | 10922 |
356 | Develop | [android] 화면 전환(가로/세로)시 설정 | hooni | 2013.04.23 | 43409 |
355 | Develop | [android] 멀티터치(Multi touch) 부분 구현 ㅋㅋ | hooni | 2013.04.23 | 27453 |
354 | Develop | [html] HTML5 튜토리얼 링크 ㅋㅋ | hooni | 2013.04.23 | 12822 |
353 | Develop | [android] 해상도 관련 팁 (dip -> pixel 변환) | hooni | 2013.04.23 | 15357 |
352 | Develop | [android]개발 가이드 및 한글화 문서 | hooni | 2013.04.23 | 47552 |
351 | Develop | [pdf] GPS의 동작 원리 ㅎㅎ | hooni | 2013.04.23 | 23894 |
350 | Develop | [doc] UI개발시 유용한 소프트웨어 (개발 및 디버깅 툴) | hooni | 2013.04.23 | 12921 |