Develop
2013.04.23 13:15
[c] 파일(int fd)에서 개행문자 단위로 읽기 by 후리자
Views 8084 Comment 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;
}
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
349 | System/OS |
[ms-sql] 프로시져 예제..
![]() |
hooni | 2013.04.23 | 14278 |
348 | Develop | [c#] 파일(File) 쓰기 ㅎㅎ | hooni | 2013.04.23 | 14294 |
347 | Develop |
[ios][swift] 초간단 카달로그 앱 (Catalog App)
![]() |
hooni | 2016.06.27 | 14307 |
346 | System/OS | [linux] 간단한 vi편집기 사용 명령 | hooni | 2003.04.23 | 14378 |
345 | System/OS | [linux] Xwindow/Xmanager 사용 | hooni | 2003.04.23 | 14392 |
344 | System/OS | [linux] 기존 환경설정 저장하면서 커널 컴파일.. | hooni | 2003.04.23 | 14441 |
343 | System/OS | [linux] ipchains 옵션 | hooni | 2003.04.23 | 14446 |
342 | Develop | [html] 캐쉬된 웹페이지 사용하지 않도록 하는 방법 | hooni | 2003.04.23 | 14446 |
341 | Develop | [js] 자바스크립트를 동적으로 로딩하기 | hooni | 2013.04.23 | 14454 |
340 | System/OS | [linux] 쉘 스크립트에 대한 설명과 예제.. | hooni | 2003.04.23 | 14479 |
339 | System/OS | [linux] 셀 스크립트 if, for, case in.. | hooni | 2003.04.23 | 14493 |
338 | Develop |
[c] flooding 알고리즘 미로 찾기(도스용) 소스코드
9 ![]() |
hooni | 2003.04.23 | 14516 |