Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 8087 댓글 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 | System/OS | [sql] 내 방명록 답글 찾는 쿼리문.. (JOIN 구문) | hooni | 2003.04.23 | 13777 |
372 | System/OS | [linux] Proftpd 설치 가이드 | hooni | 2003.04.23 | 13846 |
371 | Develop |
[js] AngularJS를 소개합니다.
![]() |
hooni | 2014.01.06 | 13848 |
370 | System/OS | [linux] 아파치 설치/설정(proxy) | hooni | 2003.04.23 | 13850 |
369 | System/OS | [linux] 콘솔 기본언어 설정 방법 | hooni | 2013.04.23 | 13850 |
368 | Develop |
[php] 심플한 게시판 ㅋㅋ
1 ![]() |
hooni | 2013.04.23 | 13860 |
367 | Develop |
[c++] 자료구조(링크리스트,스택,큐)와 후위 표기 계산기 샘플 ㅋㅋ
4 ![]() |
hooni | 2013.04.23 | 13892 |
366 | Etc | 선과 악에 대한 영어논술문항(지킬앤하이드 독서 후 이어지는 심화 수행평가) | hooni | 2013.12.04 | 13909 |
365 | System/OS | [linux] 아파치설치/설정 - SSI(Server Side Include) | hooni | 2003.04.23 | 13940 |
364 | Develop |
[c] 다중연결 서버 만들기 #1 - fork() 사용
![]() |
hooni | 2013.04.23 | 13945 |
363 | System/OS | [linux] ipfwadm를 이용한 패킷필터링(구버전) | hooni | 2003.04.23 | 13951 |
362 | System/OS | [linux] ProFTPD 타임아웃 설정 | hooni | 2003.04.23 | 13955 |