C언어에서 용량큰 파일 read할때

rriiaa의 이미지

안녕하세요? 다들 무더운 여름 아무탈 없이 잘보내셨나요?

다름이 아니라 C로 파일의 내용을 읽어서 처리하는 프로그램을 짜고 있는데

읽어야할 파일 용량이 상당히 큽니다. fopen 함수를 사용해서 while((read = getline(&line,&length,table_file))

-->가장 많이 사용하시는거죠 getline으로 파일을 읽고 있는데 읽어야할 파일이 크다보니

killed 되버리네요 ㅜㅜ 스크립트 짜서 파일을 나누고 여러번 실행 시켜도 되기는 하는데 혹시나

용량 큰파일 나눠서 읽는 좋은 방법이 없나해서 이렇게 질문드립니다.

bushi의 이미지

> 스크립트 짜서 파일을 나누고 여러번 실행 시켜도 되기는 하는데
>

적으신 것만으로 추정해보면, 아마 그렇게해도 안될겁니다.
파일의 크기가 문제가 아니라 한 줄의 길이가 긴 것이 문제라 생각됩니다.

읽기만 할 것이라면, 그런 식으로 읽을 필요 없습니다.
https://ko.wikipedia.org/wiki/메모리_맵_파일

라스코니의 이미지

while((read = getline(&line,&length,table_file)) 을 할 때 읽어드리는 버퍼의 길이가 충분한가요? line 이 char *line = NULL 등으로 NULL로 선언되어 있는지 확인해 보세요.

문제가 없다면 64 bit 컴파일러로 하고 계신가요?

Anti-Lock의 이미지

다음의 문서에 의하면, 파일 끝까지 다 읽으면 getline 리턴값은 -1 인것 같네요.

http://man7.org/linux/man-pages/man3/getline.3.html

다음은 해당 페이지의 샘플코드입니다.

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
 
int
main(void)
{
   FILE *stream;
   char *line = NULL;
   size_t len = 0;
   ssize_t read;
 
   stream = fopen("/etc/motd", "r");
   if (stream == NULL)
	   exit(EXIT_FAILURE);
 
   while ((read = getline(&line, &len, stream)) != -1) {
	   printf("Retrieved line of length %zu :\n", read);
	   printf("%s", line);
   }
 
   free(line);
   fclose(stream);
   exit(EXIT_SUCCESS);
}

사족) 어쨌거나 파일을 전부 버퍼에 올릴거라면, 파일 사이즈만큼 퍼버를 미리 확보하면 더 빠를것 같습니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.