Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
조회 수 8085 댓글 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;
}
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
793 | Develop | [ios] GCD 변수 사용 예제 | hooni | 2013.10.01 | 13575 |
792 | System/OS | [linux] 프로세스 상태확인(ps) | hooni | 2003.04.23 | 13544 |
791 | Develop |
[js] 네이버, 다음의 플레이어 스킨
![]() |
hooni | 2013.04.23 | 13535 |
790 | System/OS |
[doc] 피쳐셀렉션(feature selection using..) 발표 자료
![]() |
hooni | 2013.04.23 | 13531 |
789 | Develop |
[c++] Win32 API 기본 출력인 MessageBox() 함수 사용 예제..
![]() |
hooni | 2013.04.23 | 13517 |
788 | PPT |
[ppt] 네오웹 소프트.. 발표 자료 모음
![]() |
hooni | 2003.04.23 | 13517 |
787 | Develop | [c] 문자열 자르는 함수(strtok) 예제 | hooni | 2013.04.23 | 13502 |
786 | Develop |
[ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동
![]() |
hooni | 2014.01.24 | 13483 |
785 | System/OS | [unix] SUN Solaris 싱글모드.. ㅡ,.ㅡ; | hooni | 2003.04.23 | 13433 |
784 | System/OS |
[linux] iconv를 이용하여 euc-kr 문서를 utf-8로 대량으로 변환하기
![]() |
hooni | 2014.01.09 | 13430 |
783 | Database | [mysql] DB->Text, Text->DB 변환 | hooni | 2003.04.23 | 13415 |
782 | Develop |
[c] 파일입출력, 링크리스트(linked list)를 이용한 주소록(도스용) 소스코드
1 ![]() |
hooni | 2003.04.23 | 13403 |