Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 7162 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
625 | Develop | [ios] NSString 간단한 정규식 사용법 | hooni | 2014.01.28 | 18264 |
624 | Develop |
[ios] iOS 6.0 이상 회전 하기 (이전 버전과 비교 변경 부분)
![]() |
hooni | 2014.01.27 | 34237 |
623 | Develop |
[ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동
![]() |
hooni | 2014.01.24 | 12518 |
622 | Develop |
[js] 주소표시줄 URL 읽어오기 (변경까지)
![]() |
hooni | 2014.01.21 | 1 |
621 | Develop | [ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기 | hooni | 2014.01.16 | 14902 |
620 | Develop | [ios] UIWebView 쿠키 유지 | hooni | 2014.01.16 | 11750 |
619 | Develop | [php] substr() 한글 자를 때 깨짐 방지 | hooni | 2014.01.09 | 20438 |
618 | Develop | [ios] UIButton multi-line iOS7 | hooni | 2014.01.09 | 11460 |
617 | Develop |
[js] AngularJS를 소개합니다.
![]() |
hooni | 2014.01.06 | 13050 |
616 | Develop | [php] 하루 전 날짜 쉽게 구하기. | hooni | 2013.12.25 | 12278 |
615 | Develop | [php] GregorianToJD(), JDToGregorian() 함수 내용 | hooni | 2013.12.25 | 10635 |
614 | Develop | [js] jQuery plugin 요약 | hooni | 2013.12.20 | 10748 |