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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
409 | Develop | [jsp] 페이지에 한글이 정상적으로 보이지 않을 때.. | hooni | 2013.04.23 | 9271 |
408 | Develop | [asp] 기본 문법과 제어문 | hooni | 2013.04.23 | 8288 |
407 | Develop | [asp] 문자열 관련 함수 요약 | hooni | 2013.04.23 | 9157 |
406 | Develop | [asp] 문자열 넘겨받기 (get,post) | hooni | 2013.04.23 | 9821 |
405 | Develop | [php] 정규표현식 간단히 정리 | hooni | 2013.04.23 | 7822 |
404 | System/OS | [linux] 콘솔 기본언어 설정 방법 | hooni | 2013.04.23 | 13850 |
403 | Develop | [js] selectbox 선택 후 input 박스에 적용 | hooni | 2013.04.23 | 10443 |
402 | Develop |
[ajax] 이벤트 코드 생성기 작업중.. ㅋㅋ
![]() |
hooni | 2013.04.23 | 8566 |
401 | System/OS | [sql] 중복데이터 삭제 쿼리 | hooni | 2013.04.23 | 15767 |
400 | System/OS | [sql] 조회구문(select)에서 중복 데이터를 한 번만 출력 (distinct) 1 | hooni | 2013.04.23 | 35651 |
399 | Develop | [php] 날짜 정보 출력 (년,월,일,시,분,초) | hooni | 2013.04.23 | 11616 |
398 | Develop |
[c] KNN(K-nearest neighbor) 패턴인식 과제..
![]() |
hooni | 2013.04.23 | 8562 |