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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
585 | Develop | [js] 쿠키(cookie)에 대한 설명과 예제.. | hooni | 2003.04.23 | 8320 |
584 | Develop | [c++] 레지스트리 편집하는 부분..ㅡㅡ; | hooni | 2013.04.23 | 8267 |
583 | Develop | [c++] 현승이가 준 메신저 소스.. ㅋㅋ | hooni | 2013.04.23 | 8260 |
582 | Develop | [c] selec()를 이용한 입출력 다중화 | hooni | 2013.04.23 | 8259 |
581 | Develop | [c] 이진트리(binary tree)의 운행.. | hooni | 2003.04.23 | 8249 |
580 | Develop | [php] 마시마로 캐릭터 방명록 | hooni | 2003.04.23 | 8239 |
579 | Develop | [c] 문자열 정렬 함수 qsort() | hooni | 2003.04.23 | 8228 |
578 | Develop | [c] 그래픽 차트 라이브러리.. 나중에 확인 해볼 거.. | hooni | 2013.04.23 | 8225 |
577 | Develop | [unix] Java 애플릿용 HTML 자동 생성 | hooni | 2014.02.19 | 8210 |
576 | Develop | [c] 스토리지 클래스(변수) | hooni | 2003.04.23 | 8197 |
575 | Develop | [vb] 64bit RSA 프로그램 소스 ㅋㅋ | hooni | 2013.04.23 | 8181 |
574 | Develop | [c] 프로그래밍 ppt, 스킬업 (비트 수업자료) | hooni | 2003.04.23 | 8177 |