Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 7165 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
613 | Develop |
[c] 디피-헬만 키교환(Diffie–Hellman key exchange) 샘플 코드.. ㅋㄷ
![]() |
hooni | 2013.04.23 | 9127 |
612 | Develop | [switch] 시스코 스위치(catalyst 2950) telnet 설정 | hooni | 2013.04.23 | 11302 |
611 | Develop |
[c] 맵서치인 듯(옛날 컴에서 찾은 자료)
![]() |
hooni | 2013.04.23 | 6916 |
610 | Develop |
[js] 윈도우 시작버튼처럼 나오는 메뉴
![]() |
hooni | 2013.04.23 | 6766 |
609 | Develop |
[js] 네이버, 다음의 플레이어 스킨
![]() |
hooni | 2013.04.23 | 6973 |
608 | Develop |
[js] 2차 잉여.. (Quadratic reciprocity) 계산..
![]() |
hooni | 2013.04.23 | 7098 |
607 | Develop |
JSON, BSON 변환
![]() |
hooni | 2013.04.23 | 11874 |
606 | Develop | [c] GD라이브러리(jpeg)를 사용한 웹 카운터 샘플 | hooni | 2013.04.23 | 15128 |
605 | Develop |
[c] 민수형 소스(도메인소켓포함)
![]() |
hooni | 2013.04.23 | 6547 |
604 | Develop |
[c++] 마방진 소스코드
![]() |
hooni | 2013.04.23 | 9570 |
603 | Develop |
[c] 소켓 스트림 서버/클라이언트 (UDP)
![]() |
hooni | 2013.04.23 | 7615 |
602 | Develop |
[c] 웅지학원 NAT를 소스코드로..
![]() |
hooni | 2013.04.23 | 6625 |