Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 8084 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
327 | Develop | [php] 초간단 게시판 페이지 분할 알고리즘 | hooni | 2003.04.23 | 10117 |
326 | Develop |
[c] 디피-헬만 키교환(Diffie–Hellman key exchange) 샘플 코드.. ㅋㄷ
![]() |
hooni | 2013.04.23 | 10127 |
325 | Develop |
[c] 도메인 소켓(Unix Domain Socket) UDP
![]() |
hooni | 2013.04.23 | 10173 |
324 | Develop |
[c++] MFC로 만든 디렉토리/파일 파인더
![]() |
hooni | 2013.04.23 | 10177 |
323 | Develop |
[ios] UUID 생성 + Key Chain 연동
![]() |
hooni | 2016.05.13 | 10187 |
322 | Develop | [js] jQjuery $ 활용 | hooni | 2013.12.17 | 10189 |
321 | Develop |
[java] RGB 색상 조절 버튼.. 스윙(swing)
![]() |
hooni | 2013.04.23 | 10207 |
320 | Develop | 다운 받아서 테스트 해볼것.. | hooni | 2013.04.23 | 10237 |
319 | Develop |
[c++] mfc이용한 트레이아이콘(TrayIcon) 클래스 예제 프로젝트
![]() |
hooni | 2013.04.23 | 10254 |
318 | Develop | [js] jQuery 셀랙터(selector) 요약 | hooni | 2013.12.17 | 10266 |
317 | Develop | [c] 약수/최대공약수/완전수 알고리즘 | hooni | 2003.04.23 | 10271 |
316 | Develop |
[c] 다중연결 서버 만들기 #2 - select() 사용
![]() |
hooni | 2013.04.23 | 10287 |