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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
849 | Develop | [c] 64bit 머신에서 inet_ntoa() 사용시 Segment fault 대처 방법법 | hooni | 2014.02.08 | 14251 |
848 | Develop | [ios] NSString 간단한 정규식 사용법 | hooni | 2014.01.28 | 18249 |
847 | Develop | [ios] iOS 6.0 이상 회전 하기 (이전 버전과 비교 변경 부분) | hooni | 2014.01.27 | 34229 |
846 | Develop | [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동 | hooni | 2014.01.24 | 12508 |
845 | Develop | [js] 주소표시줄 URL 읽어오기 (변경까지) | hooni | 2014.01.21 | 1 |
844 | System/OS | [linux] CentOS 6.x Cati 설치 (yum) | hooni | 2014.01.17 | 48644 |
843 | Develop | [ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기 | hooni | 2014.01.16 | 14894 |
842 | Develop | [ios] UIWebView 쿠키 유지 | hooni | 2014.01.16 | 11736 |
841 | Develop | [php] substr() 한글 자를 때 깨짐 방지 | hooni | 2014.01.09 | 20426 |
840 | System/OS | [linux] iconv를 이용하여 euc-kr 문서를 utf-8로 대량으로 변환하기 | hooni | 2014.01.09 | 12273 |
839 | Develop | [ios] UIButton multi-line iOS7 | hooni | 2014.01.09 | 11453 |
838 | Etc | iOS 에서 쓸만한 오프라인 구글지도 찾기 | hooni | 2014.01.06 | 16079 |