473,410 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

How to read data from several files into a single buffer?

Hi everyone!!!

I want to read data from several files into a single buffer. How can I
do this???
I have something like this:

//...
typedef unsigned short word;
const unsigned int W_SIZE = sizeof(word);
//...
void RecoveryFile(int nfiles)
{
//I know the number of files (rows) and its path.
//Every path is stored in a list and I can access them as list[i]
//aditionally every file size is blocksize.

word *mydata = (word*) calloc(rows*blocksize,W_SIZE);
if (mydata == NULL) { perror("malloc - mydata"); exit(1);}

for(i=0; i<nfiles; i++)
{
f = fopen(list[i], "rb"); // I need the binary mode
if (f == NULL) { perror(" file missing"); exit(1);}
else
{
fread(??????,W_SIZE,(blocksize/2),f);//How???
}
fclose(f);
}
//... process mydata
free(mydata);
}

Any help would be very much appreciated
Thanks in advance.

--DMA

Nov 15 '05 #1
4 1691
gregus wrote:
Hi everyone!!!

I want to read data from several files into a single buffer. How can I
do this???
I have something like this:

//...
typedef unsigned short word;
"Word" is a ridiculously overloaded term that should be abolished, but
I'll assume you know what you're doing here.

In particular, "unsigned short" is only a 16-bit integer if it happens
to be that way on your particular system. If a typename for a 16-bit
integer is what you're after, give it a more descriptive name, like
`uint16' or suchlike.
const unsigned int W_SIZE = sizeof(word);
No. The result of sizeof is a size_t (obtained by including <stdlib.h>).
That may be an unsigned int, but need not be.

This constant is also completely superfluous. Just write "sizeof word"
when you need it. This may be slightly more keystrokes, but "W_SIZE"
isn't any more descriptive than "sizeof word", and you save the reader
of your code a lookup by using that directly. Trust me, this pays off.
void RecoveryFile(int nfiles)
{
//I know the number of files (rows) and its path.
//Every path is stored in a list and I can access them as list[i]
//aditionally every file size is blocksize.

word *mydata = (word*) calloc(rows*blocksize,W_SIZE);
Lose the cast. It's unnecessary and can mask an error. I assume that
when you say "every file is blocksize" you mean that every file contains
(blocksize * sizeof word) bytes, because this is what the calloc() implies.
if (mydata == NULL) { perror("malloc - mydata"); exit(1);}
If you're just signaling generic failure, use the portable EXIT_FAILURE,
not 1.
for(i=0; i<nfiles; i++)
nfiles? Is rows == nfiles or isn't it? If it is, why are you using two
variables? If it isn't, make sure that at least nfiles <= rows, or
you'll have memory problems.
{
f = fopen(list[i], "rb"); // I need the binary mode
if (f == NULL) { perror(" file missing"); exit(1);}
else
{
fread(??????,W_SIZE,(blocksize/2),f);//How???

<snip>

If I've read your intentions correctly so far, you'll want something
like this:

if (fread(mydata + i * blocksize, sizeof word, blocksize, f) < blocksize) {
perror("File less than blocksize");
fclose(f);
exit(EXIT_FAILURE);
}

Note that just exiting the program right in the middle of a function is
ugly. Again, I assume you know what you're doing. If you have something
more meaningful to do for files less than `blocksize' words, adjust
accordingly.

The division you've put in suggests that "blocksize" does not give the
size in words but in half words (supposed to be 8-bit bytes, I take it?)
Either the calloc() or the fread() call is wrong in this case.

S.
Nov 15 '05 #2
Skarmander <in*****@dontmailme.com> writes:
gregus wrote:

[...]
const unsigned int W_SIZE = sizeof(word);


No. The result of sizeof is a size_t (obtained by including
<stdlib.h>). That may be an unsigned int, but need not be.


Using unsigned int here is clumsy, but not incorrect. The result of
sizeof will be implicitly converted to unsigned int, and as long as
sizeof(word)<=32767, there's no possibility of an overflow.

But yes, size_t is the proper type to use for sizes. For one thing,
it saves you the effort of proving to yourself that no overflow is
possible.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3
Keith Thompson wrote:
Skarmander <in*****@dontmailme.com> writes:
gregus wrote:
[...]
const unsigned int W_SIZE = sizeof(word);


No. The result of sizeof is a size_t (obtained by including
<stdlib.h>). That may be an unsigned int, but need not be.

Using unsigned int here is clumsy, but not incorrect. The result of
sizeof will be implicitly converted to unsigned int, and as long as
sizeof(word)<=32767, there's no possibility of an overflow.


You're right. I didn't even consider that. I'm mentally hardwired to
treat types as opaque whenever I can, so I don't even bother thinking
about what a size_t could or couldn't contain until/unless I have to. In
this case, `unsigned int' would work.
But yes, size_t is the proper type to use for sizes. For one thing,
it saves you the effort of proving to yourself that no overflow is
possible.


In particular, adding `size_t's together will overflow only when
`size_t's do overflow. If you're using unsigned ints, you may lose out
quite a bit sooner.

S.
Nov 15 '05 #4
Thanks....

Really rows=nfiles, I'm sorry. I forgot to mention it. My code is
working very well now.

I'll have into account all your advices from now on. I already did all
these changes and everything is OK.

Again, thank you very much!!!

Nov 15 '05 #5

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

Similar topics

21
by: Gavin | last post by:
Hi, I'm a newbie to programming of any kind. I have posted this to other groups in a hope to get a response from anyone. Can any one tell me how to make my VB program read the Bios serial number...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
7
by: Mike | last post by:
Hi, I have an iteration to retrieve a number of messages from a server. Within this iteration, I am using the following code: do { readBytes = base.GetStream().Read(received, 0,...
3
by: Amy L. | last post by:
Is there a Buffer size that is optimial based on the framework or OS that is optimal when working with chunks of data? Also, is there a point where the buffer size might not be optimal (too...
6
by: comp.lang.php | last post by:
if (!function_exists('bigfile')) { /** * Works like file() in PHP except that it will work more efficiently with very large files * * @access public * @param mixed $fullFilePath * @return...
3
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
12
by: Sean Davis | last post by:
I am working on a simple script to read from one database (oracle) and write to another (postgresql). I retrieve the data from oracle in chunks and drop the data to postgresql continuously. The...
2
by: duylam76 | last post by:
I'm programming a server and corresponding client application that sends data to each other over a socket. I want to encode an integer to send over the socket. Right now everything is fine because...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
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...
0
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...

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.