Views 7152 Votes 0 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
#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
No. Category Subject Author Date Views
25 Develop [c++] p.47 연습문제 3번 hooni 2003.04.23 10105
24 Develop [opengl] 컴퓨터 그래픽스 강의 자료(수업자료) file hooni 2003.04.23 20432
23 Develop [c] 그래픽스 자료(OpenGL 라이브러리) 샘플 소스 file hooni 2003.04.23 10181
22 Develop [c] 간단한 채팅(클라이언트/서버) 프로그램 소스 file hooni 2003.04.23 9068
21 Develop [c] 팩토리얼.. - 재귀함수 hooni 2003.04.23 9500
20 Develop [c] 하노이탑 - 재귀함수 hooni 2003.04.23 9153
19 Develop [c] 소수점 반올림^^ hooni 2003.04.23 8478
18 Develop [c] 최대공약수 알고리즘 (유클릿) hooni 2003.04.23 8873
17 Develop [php] whois정보 조회 프로그램 hooni 2003.04.23 11733
16 Develop [c] 간단한 링크드 리스트(linked list) 자료형 예제.. hooni 2003.04.23 9838
15 Develop [c] CGI Library to C^^ hooni 2003.04.23 8421
14 Develop [c] scanf(), printf() 포맷의 형변환 hooni 2003.04.23 10856
13 Develop [c] home env stack overflow hooni 2003.04.23 11515
12 Develop [c] 파일입출력 간단한 설명 hooni 2003.04.23 11243
11 Develop [C] C언어의 조건 연산자(Conditional Operator) hooni 2003.04.23 14227
10 Develop [vb] 비쥬얼 베이직으로.. hooni 2003.04.23 52997
Board Pagination Prev 1 ... 49 50 51 52 53 Next
/ 53