Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
Views 7152 Comment 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;
}
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
369 | [ios] 인앱결제 & 오토레이아웃 관련 강좌 | hooni | 2013.11.14 | 0 | |
368 | Develop | [java] 초간단 싱글톤(Singleton) 패턴 샘플 코드 | hooni | 2013.11.18 | 11593 |
367 | Develop | [php] php5.3부터는 eregi()대신 preg_match()를 사용 | hooni | 2013.11.18 | 12468 |
366 | Develop | [js] window.open() 속성 사용 방법 | hooni | 2013.11.18 | 13588 |
365 | Develop | [ios] iphone SetDeviceOrientation 화면 강제 회전 | hooni | 2013.11.20 | 18458 |
364 | Develop | [ios] iOS In App Purchase #1 (코드 구현 전 웹 설정 작업) | hooni | 2013.11.20 | 14508 |
363 | Develop | [ios] iOS In App Purchase #2 (코드 구현) | hooni | 2013.11.20 | 13766 |
362 | Develop | [ios] iOS In App Purchase 코드 부분 샘플 1 | hooni | 2013.11.20 | 11758 |
361 | Develop | [ios] iOS In App Purchase 코드 부분 샘플 2 | hooni | 2013.11.20 | 11274 |
360 | Develop | [ios] In App Purchase 개발 | hooni | 2013.11.20 | 9264 |
359 | Develop | 이어서 작업할 내용~ | hooni | 2013.11.21 | 0 |
358 | Etc | [english] 영어공부 혼자 하기, 인터넷으로 영어공부하기 추천사이트 20선 | hooni | 2013.11.25 | 9408 |