3.9 G 파일을 open 하려고 합니다.

lunarainbow의 이미지

        SourceFP = fopen(argv[1], "r");
        if ( SourceFP == NULL )
        {
                printf("%s 안열린데..\n", argv[1]);
                exit(0);
        }

이처럼 아주 평범하게 오픈을 하려 합니다.

그런데 문제는 파일 크기가 3.9G 의 텍스트 파일 이라는 것인데,

덕분에 계속 실패하게 됩니다.

어떻게 해줘야 할까요?

들리는 소문에 의하면,

#define _FILE_OFFSET_BITS 64

이런것을 선언해 주면 가능해 진다고 하던데, 역시나 더군요... :(

텍스트 파일로부터 1라인씩 가져와 작업을 해야 하는데, 귀찮더라도 굳이 fopen 말고, open 함수를 이용해야 할까요??

체스맨의 이미지

stdio.h 에 fopen64 함수가 있으면 그것을 쓰시면 될 것 같은데요.

다른 방법으로는, 테스트는 안해봤는데, open (또는 open64)로 2GB이상의 파일을 열고, fdopen 을 쓰는 것도 가능할 지 모르겠구요.

Orion Project : http://orionids.org

sangheon의 이미지

이것으로 한번 해보세요.

Quote:
fopen64(3)

fopen64, freopen64
Opens a stream
LIBRARY

Standard C Library (libc.a)

Reentrant Library (libc_r.a)

SYNOPSIS


#include <stdio.h>

FILE *fopen64 (
const char (*path,
const char (*type );

FILE *freopen64 (
const char *path,
const char *type,
FILE *stream );

PARAMETERS

path

Points to a character string that contains the name of the file to be opened. If the final component of the path parameter specifies a symbolic link, the link is traversed and pathname resolution continues.

mode

Points to a character string that has one of the following values:

r or rb
Open file for reading.

w or wb
Truncate to zero length or create file for writing..

a or ab
Append; open or create file for writing at end-of-file.

r+ or rb+ or r+b
Open file for update (reading and writing).

w+ or wb+ pr w+b
Truncate to zero length or create file for update.

a+ or ab+ or a+b
Append; open or create file for update, writing at end-of-file.
The system does not distinguish between text and binary files.

stream

Specifies the input stream.

filedes

Specifies a valid open file descriptor. Note that the largest value that can be represented correctly in an object of type off64_t is established as the offset maximum in the open file description.

DESCRIPTION

The fopen64() function opens the file named by the path parameter and associates a stream with it, returning a pointer to the FILE structure of this stream. Unlike the fopen() function, the fopen64() function is Large File aware, and can access files that are larger than 2GB-1.

When you open a file for update, you can perform both input and output operations on the resulting stream. However, an output operation cannot be directly followed by an input operation without an intervening fflush() function call or a file positioning operation (fseek(), fsetpos64(), or rewind function). Also, an input operation cannot be directly followed by an output operation without an intervening flush or file positioning operation, unless the input operation encounters the end of the file.

When you open a file for append (that is, when the type parameter is a or a+), it is impossible to overwrite information already in the file. You can use the fseek() function to reposition the file pointer to any position in the file, but when output is written to the file, the current file pointer is ignored. All output is written at the end of the file and the file pointer is repositioned to the end of the output.

If two separate processes open the same file for append, each process can write freely to the file without destroying the output being written by the other. The output from the two processes is intermixed in the order in which it is written to the file. Note that if the data is buffered, it is not actually written until it is flushed.

When opened, a stream is fully buffered if and only if it can be determined not to refer to an interactive device. The error and End-of-File indicators for the stream are cleared.

If the type parameter is w, a, w+, or a+ and the file did not previously exist, upon successful completion the fopen64() function marks the st_atime , st_ctime and st_mtime fields of the file and the st_ctime and st_mtime fields of the parent directory for update. If the type parameter is w or w+ and the file did previously exist, upon successful completion the fopen64() function marks the st_ctime and st_mtime fields of the file for update.

The freopen64() function substitutes the named file in place of the open stream. The original stream is closed regardless of whether the open() function succeeds with the named file. The freopen64() function returns a pointer to the FILE structure associated with the stream parameter. The freopen64() function is typically used to attach the preopened streams associated with stdin, stdout, and stderr to other files.

Like fopen64() , freopen64() is Large File Aware, and can access files greater than 2GB-1.

NOTES

POSIX: w and w+ types do not truncate.

The fopen64() and freopen64() functions are supported for element parallel applications; to use a reentrant version, link with libc_r. See f77(1), f90(1) or cc(1) for more information.

AES Support Level:
Full use
RETURN VALUES

If one of these functions fails, it returns a null pointer and may set errno to indicate the error.

ERRORS

fopen64() Function

In case of failure, fopen64() may set errno to one of the following values:

[EACCES]
Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by the type parameter are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.

[EINTR]
The fopen64() function was interrupted by a signal which was caught.

[EINVAL]
The type parameter is not a valid mode.

[EISDIR]
The named file is a directory and type requires write access.

[ELOOP]
Too many links were encountered in translating path .

[EMFILE]
FOPEN_MAX file descriptors are currently open in the calling process.

[ENAMETOOLONG]
A component of a pathname exceeded PATH_MAX characters; an entire pathname exceeded NAME_MAX characters; or the resolution of a symbolic link exceeded PATH _MAX or NAME_MAX.

[ENFILE]
Too many files are currently open in the system.

[ENOENT]
The named file does not exist or the path parameter points to an empty string.

[ENOSPC]
The directory or file system that would contain the new file cannot be expanded.

[ENOTDIR]
A component of the path prefix is not a directory.

[ENXIO]
The named file is a character special or block special file and the device associated with this special file does not exist.

[EROFS]
The named file resides on a read only file system and type requires write access.

[ETXTBSY]
The requested file is currently opened for writing by some process.
freopen64() Function

The freopen64() function fails if the following is true:

[EACCES]
Search permission is denied on a component of the path prefix, or the file exists and the permissions specified by the type parameter are denied, or the file does not exist and write permission is denied for the parent directory of the file to be created.

[EINTR]
The freopen64() function was interrupted by a signal that was caught.

[EINVAL]
The type parameter is not a valid type.

[EISDIR]
The named file is a directory and type requires write access.

[ELOOP]
Too many symbolic links were encountered in translating path .

[EMFILE]
FOPEN_MAX file descriptors are currently open in the calling process.

[ENAMETOOLONG]
A component of a pathname exceeded PATH_MAX characters; an entire pathname exceeded NAME_MAX characters; or the resolution of a symbolic link exceeded PATH _MAX or NAME_MAX.

[ENFILE]
Too many files are currently open in the system.

[ENOENT]
The named file does not exist or the path parameter points to an empty string.

[ENOSPC]
The directory or file system that would contain the new file cannot be expanded.

[ENOTDIR]
A component of the path prefix is not a directory.

[ENXIO]
The named file is a character special or block special file, and the device associated with this special file does not exist.

[EROFS]
The named file resides on a read only file system and type requires write access.

[ETXTBSY]
The requested file is currently opened for writing by some process.
RELATED INFORMATION

Functions:
open(2), open64(2), fclose(3), fopen(3), fseek(3), setbuf(3)

--------------------------------------------------------------------------------
All rights reserved, Copyright (C) 1999, Hitachi, Ltd.
Copyright (C) 1990, Open Software Foundation, Inc.

--

Minimalist Programmer

lunarainbow의 이미지

감사합니다. ^^

fopen64 라는 함수를 사용하니, 바로 해결 되네요.

이런 함수가 있는줄 미처 몰랐었네요.

그런데 fopen64 라는 함수는 리턴값 케스팅을 해줘야 하는것 맞는지 모르겠네요..

fp = (FILE*)fopen64(argv[1], "r");

이렇게 하지 않으면 워닝이 발생하네요. ^^

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.