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] wget 명령 사용 예제 | hooni | 2020.05.26 | 1434 |
368 | System/OS | [linux] Xwindow/Xmanager 사용 | hooni | 2003.04.23 | 13332 |
367 | System/OS | [linux] X환경 GNOME에서 KDE로 바꾸는 법.. | hooni | 2013.04.23 | 12365 |
366 | System/OS | [linux] yum 업데이트 시 커널 제외하기 | hooni | 2014.09.11 | 1256 |
365 | System/OS | [linux] 간단한 find 명령어 설명(업데이트 해야 함) | hooni | 2013.04.23 | 9111 |
364 | System/OS | [linux] 간단한 NAT 설정 script | hooni | 2003.04.23 | 12597 |
363 | System/OS | [linux] 간단한 vi편집기 사용 명령 | hooni | 2003.04.23 | 13456 |
362 | System/OS | [linux] 계정관리하기(내공쌓기) | hooni | 2003.04.23 | 13157 |
361 | System/OS | [linux] 기본 명령어 (내공쌓기) | hooni | 2003.04.23 | 14333 |
360 | System/OS | [linux] 기본적인 설정하기(내공쌓기) | hooni | 2003.04.23 | 13993 |
359 | System/OS | [linux] 기존 환경설정 저장하면서 커널 컴파일.. | hooni | 2003.04.23 | 13458 |
358 | System/OS | [linux] 꿀통(honeyd) 설치하기.. 메뉴얼 과정 6 | hooni | 2006.04.23 | 20219 |