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] 네이버에서 그림 퍼올 때.. URL Encoding 관련ㅋㅋ | hooni | 2013.04.23 | 7208 |
584 | Develop | [php] 그래프 그리는 프로그램.. | hooni | 2013.04.23 | 8456 |
583 | Develop | [c#] 툴바 소스.. 개인적으로 만드는거.. | hooni | 2013.04.23 | 3304 |
582 | Develop | [js] 부모창에서 자식창으로 문자열 전달.. | hooni | 2013.04.23 | 7444 |
581 | Develop | [js] 이미지 사이즈를 동적으로 조절.. | hooni | 2013.04.23 | 7459 |
580 | Develop | [c++] 가짜 인증서(하나은행) 프로그램 | hooni | 2013.04.23 | 10746 |
579 | Develop | [c++] 디렉토리 내의 파일 찾기 FindFirstFile() 함수 6 | hooni | 2013.04.23 | 20564 |
578 | Develop | [c++] 웹폼(webform)전송과 http 파일 업로드 샘플 | hooni | 2013.04.23 | 33059 |
577 | Develop | [doc] 유료 방송 제어 시스템 (CAS) 발표자료 | hooni | 2013.04.23 | 8495 |
576 | Develop | [doc] 테크니컬 레포트 (BHO 취약점을 이용한 공격) 2 | hooni | 2013.04.23 | 27066 |
575 | Develop | OPT와 CAS에 대한 자료.. (교수님 메일로 보내드린 자료..) | hooni | 2013.04.23 | 13913 |
574 | Etc | 스파이웨어(BHO) 탐지하는 방법.. | hooni | 2013.04.23 | 44395 |