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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
229 | Develop | [css] z-index에 설정할 수 있는 최대값? | hooni | 2013.12.20 | 14701 |
228 | Develop | [js] jQuery plugin 요약 | hooni | 2013.12.20 | 10698 |
227 | Develop | [php] GregorianToJD(), JDToGregorian() 함수 내용 | hooni | 2013.12.25 | 10591 |
226 | Develop | [php] 하루 전 날짜 쉽게 구하기. | hooni | 2013.12.25 | 12193 |
225 | Develop | [js] AngularJS를 소개합니다. | hooni | 2014.01.06 | 13012 |
224 | Develop | [ios] UIButton multi-line iOS7 | hooni | 2014.01.09 | 11433 |
223 | Develop | [php] substr() 한글 자를 때 깨짐 방지 | hooni | 2014.01.09 | 20361 |
222 | Develop | [ios] UIWebView 쿠키 유지 | hooni | 2014.01.16 | 11704 |
221 | Develop | [ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기 | hooni | 2014.01.16 | 14857 |
220 | Develop | [js] 주소표시줄 URL 읽어오기 (변경까지) | hooni | 2014.01.21 | 1 |
219 | Develop | [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동 | hooni | 2014.01.24 | 12488 |
218 | Develop | [ios] iOS 6.0 이상 회전 하기 (이전 버전과 비교 변경 부분) | hooni | 2014.01.27 | 34183 |