Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
Views 7152 Comment 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;
}
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
369 | System/OS | [linux] 스케쥴링 순서(nice) 변경하기 | hooni | 2003.04.23 | 12536 |
368 | System/OS | [doc] 피쳐셀렉션(feature selection using..) 발표 자료 | hooni | 2013.04.23 | 12546 |
367 | PPT | [ppt] 네오웹 소프트.. 발표 자료 모음 | hooni | 2003.04.23 | 12550 |
366 | Database | [sql] 간단한 SQL 문법 정리 | hooni | 2003.04.23 | 12551 |
365 | Etc | [flash] 맘에 드는 파이차트 | hooni | 2013.04.23 | 12590 |
364 | System/OS | [linux] 간단한 NAT 설정 script | hooni | 2003.04.23 | 12597 |
363 | Develop | [c++] Win32 API 기본 출력인 MessageBox() 함수 사용 예제.. | hooni | 2013.04.23 | 12629 |
362 | Database | [mysql] error while loading shared libraries: libmysqlclient.so.10: | hooni | 2003.04.23 | 12663 |
361 | System/OS | [linux] root 전환시 패스워드 없이 su 사용하기 | hooni | 2013.12.22 | 12685 |
360 | System/OS | [linux] 프로세스 상태확인(ps) | hooni | 2003.04.23 | 12703 |
359 | Etc | 선과 악에 대한 영어논술문항(지킬앤하이드 독서 후 이어지는 심화 수행평가) | hooni | 2013.12.04 | 12746 |
358 | Database | [mysql] 쓰레드가 계속 쌓이기 시작하고 사라지지 않을때.. | hooni | 2003.04.23 | 12787 |