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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
549 | Develop | [c++] mfc에서 윈도우 항상 위 속성 주기.. | hooni | 2013.04.23 | 11308 |
548 | Develop | [c++] Win32 API 기본 출력인 MessageBox() 함수 사용 예제.. | hooni | 2013.04.23 | 12645 |
547 | Develop | [c] 파이프(popen)로 다른 프로세스 실행결과 가져오기 | hooni | 2013.04.23 | 8401 |
546 | Develop | [c] 도메인(호스트)으로 IP정보 알아오기.. (nslookup과 비슷) | hooni | 2013.04.23 | 6934 |
545 | Develop | [c] UTF-8을 EUC-KR로 변환.. (iconv) | hooni | 2013.04.23 | 20142 |
544 | Develop | [c] 바로 보고 정리할 것.. ㅋㄷㅋㄷ | hooni | 2013.04.23 | 7139 |
543 | Develop | [c] openssl 샘플코드.. 어려움 ㅠㅠ | hooni | 2013.04.23 | 7027 |
542 | System/OS | [linux] 한글 URL 인식할 수 있게 아파치(Apache) 설정 (mod_url.c 설치) | hooni | 2013.04.23 | 13067 |
541 | Develop | [c] 가변인자 함수(printf와 같은..) | hooni | 2013.04.23 | 7179 |
540 | Develop | [c#] BHO 한샘툴바랑 동현툴바.. | hooni | 2013.04.23 | 2208 |
539 | Develop | [c++] Win32API를 이용한 ExitWindowsEx 사용한 예제코드 | hooni | 2013.04.23 | 7964 |
538 | Develop | [c#] mfc 기반의 웹서비스 서버/클라이언트 샘플과 예제 소스 | hooni | 2013.04.23 | 2073 |