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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
369 | Develop | [js] 사라지는 브라우저 | hooni | 2003.04.23 | 6857 |
368 | Develop | [js] get방식, url이후 모두 그대로 읽어오기.. | hooni | 2013.04.23 | 6841 |
367 | Develop | [pdf] 윈도우즈 95 시스템 프로그래밍(Windows 95 system programming) | hooni | 2013.04.23 | 6840 |
366 | Develop | [ajax] 샘플 코드와 한글처리에 대한 간단한 설명 | hooni | 2013.04.23 | 6840 |
365 | Develop | [js] 수명체크 프로그램 ㅋㅋ | hooni | 2013.04.23 | 6838 |
364 | Develop | [c] 문자열 str_shift 예제.. | hooni | 2013.04.23 | 6831 |
363 | Develop | [c++] 프리렉(freelec) 예제 자료.. ㅋㄷ | hooni | 2013.04.23 | 6827 |
362 | Develop | [c] 웹 메모장.. ㅋㅋ | hooni | 2013.04.23 | 6821 |
361 | Develop | [c] 단기과정[01/14] 파일 입출력 | hooni | 2003.04.23 | 6821 |
360 | Develop | [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 | hooni | 2013.04.23 | 6800 |
359 | Develop | [js] 숫자만 입력하게 하는 자바스크립트 | hooni | 2013.04.23 | 6795 |
358 | Develop | [js] ajax를 이용해 외부문서 불러오기.. | hooni | 2013.04.23 | 6792 |