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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
625 | Develop | [c] OpenGL 직사각형(2D) 크기 확대/축소 | hooni | 2003.04.23 | 10088 |
624 | Develop |
[c][cpp] mfc, win32api, 예제 소스 대박 모음~
![]() |
hooni | 2013.04.23 | 10026 |
623 | Develop |
[c][java] 주사선 채우기 알고리즘(scan line filling algorithm) 구현
![]() |
hooni | 2013.04.23 | 10026 |
622 | Develop | [c] OpenGL 마우스 이벤트 | hooni | 2003.04.23 | 10023 |
621 | Develop |
[java] 채팅창 처럼.. swing..
![]() |
hooni | 2013.04.23 | 9998 |
620 | Develop |
[c] 컴파일러 DFA구현^^ 입력 받아 실행
4 ![]() |
hooni | 2003.04.23 | 9976 |
619 | System/OS | [linux] man 명령어 뽀개기.. | hooni | 2003.04.23 | 9956 |
618 | Develop | [c] Unix Domain Socket 을 이용한 IPC | hooni | 2013.04.23 | 9953 |
617 | System/OS | [linux] 간단한 find 명령어 설명(업데이트 해야 함) | hooni | 2013.04.23 | 9943 |
616 | Develop |
[c++] 중복실행 방지(Mutex;뮤텍스 ) 샘플 소스.. ㅋㅋ
![]() |
hooni | 2013.04.23 | 9929 |
615 | Develop | [c] 이진트리(binary tree)의 운행.. | hooni | 2003.04.23 | 9929 |
614 | Develop |
[c++] mfc 윈도우에서 ODBC 사용하여 MDB파일 읽기.. ㅋㅋ
![]() |
hooni | 2013.04.23 | 9920 |