Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 8083 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
373 | Develop | [js] IE에서 인쇄 설정 팁 | hooni | 2013.04.23 | 11946 |
372 | Etc | [php] 싸이월드 이미지 외부 링크 하기(php) | hooni | 2013.04.23 | 17621 |
371 | System/OS |
[doc] 피쳐셀렉션(feature selection using..) 발표 자료
![]() |
hooni | 2013.04.23 | 13530 |
370 | System/OS |
[doc] 네트워크 장비와 라우터 설정 방법 발표 자료
![]() |
hooni | 2013.04.23 | 15209 |
369 | System/OS |
[windows] 인터넷 익스플로러(IE) 도구모음 표시줄에 아이콘 추가
![]() |
hooni | 2013.04.23 | 19086 |
368 | System/OS | [windows] 종료, 재시작, 로그아웃 아이콘 만들기 | hooni | 2013.04.23 | 19079 |
367 | System/OS | [router] 설정과 기본 명령어들 모음 | hooni | 2013.04.23 | 16451 |
366 | System/OS | NAT와 DHCP에 대한 간단한 설명 | hooni | 2013.04.23 | 19151 |
365 | System/OS | [linux] 간단한 find 명령어 설명(업데이트 해야 함) | hooni | 2013.04.23 | 9943 |
364 | Etc |
[flash] 맘에 드는 파이차트
![]() |
hooni | 2013.04.23 | 13587 |
363 | Develop |
[web] 웹 연동 프로그램 모음..
![]() |
hooni | 2013.04.23 | 7961 |
362 | System/OS |
[doc] TCP/IP 강의 자료 (html)
![]() |
hooni | 2013.04.23 | 11944 |