473,799 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reading data from a file

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?
Jan 24 '06 #1
7 2184
Ico
John Smith <JS****@mail.ne t> 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
Jan 24 '06 #2
Ico wrote:
John Smith <JS****@mail.ne t> 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.

Jan 24 '06 #3
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.
Jan 24 '06 #4
Ico wrote:
John Smith <JS****@mail.ne t> 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.
Jan 24 '06 #5
On 2006-01-24, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Ico wrote:
John Smith <JS****@mail.ne t> 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.

Jan 24 '06 #6
Jordan Abel wrote:
On 2006-01-24, Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Ico wrote:
John Smith <JS****@mail.ne t> 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.
Jan 25 '06 #7
Ico
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
Ico wrote:
John Smith <JS****@mail.ne t> 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
Jan 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
3594
by: Andy | last post by:
Hi, In the code below (not pretty I know but it's an early version :-P) I'm having problems reading the data object back in. If I move the reading code to immediately after the section where it is written ( commented out in code) then it reads in OK. However, when I move the code to the right place ( as shown here) it throws an IO exception. Both the Filter class and it's constituent class implement Serializable and it would seem the...
1
7056
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the file... Thank you to all of you who can help me with this one...
3
2210
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
1
6762
by: Magnus | last post by:
allrite folks, got some questions here... 1) LAY-OUT OF REPORTS How is it possible to fundamentaly change the lay-out/form of a report in access? I dont really know it that "difficult", but listen up; Reports, the way I look at them, all present data downwards, in this way; TITLE data
7
6063
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should populate a series of structures of specified variable composition. I have the structures created OK, but actually reading the files is giving me an error. Can I ask a simple question to start with: I'm trying to read the file using the...
4
12808
by: Amit Maheshwari | last post by:
I need to read text file having data either comma seperated or tab seperated or any custom seperator and convert into a DataSet in C# . I tried Microsoft Text Driver and Microsoft.Jet.OLEDB.4.0 to read text file but could not get the data in correct format. All columns are not coming in dataset and rows are messing up. Suggestions please ???
6
5276
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
14993
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
13
3714
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file consists of structures of type---- struct record { int acountnum; char name; float value;
6
3533
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10025
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7563
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6804
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.