Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 7163 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
405 | Develop | [c++] mfc 기반 멀티수납(wall)시스템 소스와 실행파일 | hooni | 2013.04.23 | 7078 |
404 | Develop | [c++] 소켓 프로그래밍 관련 링크.. (퍼올려고 올린거) | hooni | 2013.04.23 | 7063 |
403 | Develop | [c] 텍스트 파일(로그)을 정해진 라인 단위로 쪼개주는 코드 | hooni | 2013.04.23 | 7062 |
402 | Develop | [c] 단어 입력/수정 프로그램 소스 | hooni | 2003.04.23 | 7059 |
401 | Develop | [C++] 18일차 과제물, String 클래스 디자인 【 C++ 문제 】 | hooni | 2013.04.23 | 7055 |
400 | Develop | 도메인 관련 솔루션 분석할 거.. ㅋㄷ | hooni | 2013.04.23 | 7053 |
399 | Develop | [js] 큐 형식으로 배열사용.. ㅋㅋ | hooni | 2013.04.23 | 7044 |
398 | Develop | [c] 정사각배열의 서브 배열의 최대 값 구하기 | hooni | 2003.04.23 | 7043 |
397 | Develop | [c] 패스워드 암호화/사용자 정보 보기 | hooni | 2003.04.23 | 7040 |
396 | Develop | [c] C에서 MySQL DB 사용하기~ | hooni | 2003.04.23 | 7040 |
395 | Develop | [c] openssl 샘플코드.. 어려움 ㅠㅠ | hooni | 2013.04.23 | 7039 |
394 | Develop | [js] 이미지 미리 로딩하기 | hooni | 2003.04.23 | 7039 |