Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > reading data from a file

Reply
Thread Tools

reading data from a file

 
 
John Smith
Guest
Posts: n/a
 
      01-24-2006
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?
 
Reply With Quote
 
 
 
 
Ico
Guest
Posts: n/a
 
      01-24-2006
John Smith <(E-Mail Removed)> 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(), get the
current position with ftell(). This position is the size of the file.
Allocate the buffer, go back to the beginning of the file with rewind()
or fseek(), and use fread() to read the file into the buffer.
Don't forget to add error checking where needed.

--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
 
 
 
Flash Gordon
Guest
Posts: n/a
 
      01-24-2006
John Smith 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?


That depends on what is efficient on your implementation. One possible
way to to use realloc to increase the space allocated as you read the
file, however you have to decide on an appropriate resizing algorithm,
since growing it one byte at a time is unlikely to be efficient.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Vladimir S. Oka
Guest
Posts: n/a
 
      01-24-2006
Ico wrote:

> John Smith <(E-Mail Removed)> 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(), get the
> current position with ftell(). This position is the size of the file.
> Allocate the buffer, go back to the beginning of the file with
> rewind() or fseek(), and use fread() to read the file into the buffer.


This seems to refer to the case where the whole file is sucked into the
memory at once. Following may be a better/more flexible idea.

Presumably, data in the file is organised in some sort of records. You
can read one record at a time, get its size (if you know their size in
advance, so much the better), allocate the memory you need, and store
the record into the newly allocated memory. This approach would also
allow you to have file with records of variable size. Your dynamically
allocated array would in fact be an array of pointers to dynamically
allocated memory for individual elements. Some would call it a
collection.

> Don't forget to add error checking where needed.


This, of course, is mandatory!

Cheers

Vladimir

--
Real computer scientists don't comment their code. The identifiers are
so long they can't afford the disk space.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-24-2006
Ico wrote:
> John Smith <(E-Mail Removed)> 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.

> get the
> current position with ftell().


The, quoting from the section of the standard defining ftell we have,
"For a text stream, its file position indicator contains unspecified
information, usable by the fseek..." so on a text stream it may not be
the file size, it is just some number fseek can use.

> This position is the size of the file.


So your suggestion is not guaranteed to work for either binary or text
streams.

> Allocate the buffer, go back to the beginning of the file with rewind()
> or fseek(), and use fread() to read the file into the buffer.
> Don't forget to add error checking where needed.


I seriously think the realloc method is better in terms of portability.
It will also then cope with any non-seekable stream, such as named pipes
on systems that support such things.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      01-24-2006
On 2006-01-24, Flash Gordon <(E-Mail Removed)> wrote:
> Ico wrote:
>> John Smith <(E-Mail Removed)> 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

>
> > get the
>> current position with ftell().

>
> The, quoting from the section of the standard defining ftell we have,
> "For a text stream, its file position indicator contains unspecified
> information, usable by the fseek..." so on a text stream it may not be
> the file size, it is just some number fseek can use.
>
> > This position is the size of the file.

>
> So your suggestion is not guaranteed to work for either binary or text
> streams.
>
>> Allocate the buffer, go back to the beginning of the file with rewind()
>> or fseek(), and use fread() to read the file into the buffer.
>> Don't forget to add error checking where needed.

>
> I seriously think the realloc method is better in terms of portability.
> It will also then cope with any non-seekable stream, such as named pipes
> on systems that support such things.

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-25-2006
Jordan Abel wrote:
> On 2006-01-24, Flash Gordon <(E-Mail Removed)> wrote:
>> Ico wrote:
>>> John Smith <(E-Mail Removed)> 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.
 
Reply With Quote
 
Ico
Guest
Posts: n/a
 
      01-25-2006
Flash Gordon <(E-Mail Removed)> wrote:
> Ico wrote:
>> John Smith <(E-Mail Removed)> 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.


I wasn't aware of that.

>> get the current position with ftell().

>
> The, quoting from the section of the standard defining ftell we have,
> "For a text stream, its file position indicator contains unspecified
> information, usable by the fseek..." so on a text stream it may not be
> the file size, it is just some number fseek can use.


And didn't know that either.

Thanks for pointing that out, yet another educational moment for me on
c.l.c.


--
:wq
^X^Cy^K^X^C^C^C^C
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading a file and resuming reading. Karim Ali Python 2 05-25-2007 02:04 PM
UnauthorizedAccessException when reading XML files (no problem when reading other file-types) blabla120@gmx.net ASP .Net 0 09-15-2006 02:08 PM
An Automated process of watching a network file folder, reading a file in it and deleting the file using ASP.NET ? Luis Esteban Valencia Muņoz ASP .Net 3 06-04-2005 10:56 AM
reading the DB vs. reading a text file...performance preference? Darrel ASP .Net 3 11-11-2004 02:27 PM
reading output file data as input data Stephen Moon Perl Misc 5 02-29-2004 02:16 PM



Advertisments