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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
849 | Develop | [c++] mfc 간단한 파일 입출력 예제 | hooni | 2013.04.23 | 13529 |
848 | System/OS | [linux] 쉘 환경변수 PS1(프롬프트) | hooni | 2003.04.23 | 13495 |
847 | System/OS | [linux] 기존 환경설정 저장하면서 커널 컴파일.. | hooni | 2003.04.23 | 13469 |
846 | Develop | [c] 기본 자료형(int)이 표현할 수 없는 큰 수(Big number)를 덧셈하는 코드. 1 | hooni | 2013.04.23 | 13458 |
845 | System/OS | [linux] 간단한 vi편집기 사용 명령 | hooni | 2003.04.23 | 13456 |
844 | System/OS | [linux] 프로그램 설치방법 (내공쌓기) | hooni | 2003.04.23 | 13433 |
843 | System/OS | [linux] 쉘 스크립트에 대한 설명과 예제.. | hooni | 2003.04.23 | 13414 |
842 | System/OS | [ms-sql] 프로시져 예제.. | hooni | 2013.04.23 | 13408 |
841 | Algorithm | 스터디 자료, 암호화에 대해서.. 나중에 볼 ppt.. | hooni | 2013.04.23 | 13376 |
840 | System/OS | [linux] Xwindow/Xmanager 사용 | hooni | 2003.04.23 | 13344 |
839 | Develop | [ios] 간단한 방법으로 OS버전 확인하기. | hooni | 2013.09.24 | 13318 |
838 | Develop | [c#] 파일(File) 쓰기 ㅎㅎ | hooni | 2013.04.23 | 13300 |