Re: reading data from a file



Jordan Abel wrote:
On 2006-01-24, Flash Gordon <spam@xxxxxxxxxxxxxxxxxx> wrote:
Ico wrote:
John Smith <JSmith@xxxxxxxx> wrote:
I want to read data from a file and assign it to a dynamically allocated array. I don't know the number of data in advance. My approach has been to read the file twice, the first time to determine its size, the second for the actual assignment. Is there a more efficient way?
Open the file with fopen(), seek to the end with fseek(),
To quote from the standard, "A binary stream need not
meaningfully support fseek calls with a whence value of SEEK_END." So that part of your suggestion is not portable if this is a binary file.

does a text stream need to? you can't seek at all on stdout/stdin on some implementations

<snip>

The call is allowed to fail, as with any library call, and is likely to fail if used on stdio/stdout. However, the quote I gave reads to me that even if a binary stream is seekable you can't rely on being able to seek to its end.

There have been several discussions on here about finding a files size where all this has been mentioned before. I was just pointing out two parts of the standard which explicitly make what was suggested non-portable.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.




Relevant Pages

  • Re: Quote Mine Question for Kermit
    ... No matter how useful it is, though, the radiocarbon ... From what I found in my searches, Robert Lee is the son of Thomas ... If it can fail the first time what makes you think it will not fail the ...
    (talk.origins)
  • Re: Spartan 3 documentation confusing...
    ... If we explain it right the first time, we get less confusion, and we get ... The faster we succeed (or fail ... totally mad, and can not be trusted even for a moment ('that's not ... I have a personality: ...
    (comp.arch.fpga)
  • Re: Quote Mine Question for Kermit
    ... No matter how useful it is, though, the radiocarbon ... From what I found in my searches, Robert Lee is the son of Thomas ... If it can fail the first time what makes you think it will not fail the ...
    (talk.origins)
  • Re: Yet another [Un]Zip behavior quirk. Non-stupid opinions sought.
    ... > first time, be fully allocated at once, instead of incrementally. ... >> precautions against exceeding disk quota or filling up the disk. ... > fail to see any difference from the previous behavior. ...
    (comp.os.vms)
  • Re: legacy COM in asp.net
    ... multiple-CPU systems), then this will be the first time your COM component ... beware of using a single instance of the component stored in Session ... that it will fail if it wasn't built for that. ...
    (microsoft.public.dotnet.general)