Contents

조회 수 7152 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
#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;
}


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
849 Develop [c] 64bit 머신에서 inet_ntoa() 사용시 Segment fault 대처 방법법 hooni 2014.02.08 14235
848 Develop [ios] NSString 간단한 정규식 사용법 hooni 2014.01.28 18222
847 Develop [ios] iOS 6.0 이상 회전 하기 (이전 버전과 비교 변경 부분) file hooni 2014.01.27 34187
846 Develop [ios] 네트워크 인디케이터(NetworkActivityIndicator) 작동 file hooni 2014.01.24 12493
845 Develop [js] 주소표시줄 URL 읽어오기 (변경까지) secret hooni 2014.01.21 1
844 System/OS [linux] CentOS 6.x Cati 설치 (yum) hooni 2014.01.17 48596
843 Develop [ios] UIWebView에서 NSURLRequest에 Cookie 실어 보내기 hooni 2014.01.16 14861
842 Develop [ios] UIWebView 쿠키 유지 hooni 2014.01.16 11708
841 Develop [php] substr() 한글 자를 때 깨짐 방지 hooni 2014.01.09 20367
840 System/OS [linux] iconv를 이용하여 euc-kr 문서를 utf-8로 대량으로 변환하기 file hooni 2014.01.09 12221
839 Develop [ios] UIButton multi-line iOS7 hooni 2014.01.09 11437
838 Etc iOS 에서 쓸만한 오프라인 구글지도 찾기 hooni 2014.01.06 16038
Board Pagination Prev 1 ... 23 24 25 26 27 28 29 30 31 32 ... 98 Next
/ 98