473,657 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t 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*blo cksize,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 1714
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(in t 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*blo cksize,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_FAILU RE);
}

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*****@dontma ilme.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)<=3 2767, 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_Keit h) 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*****@dontma ilme.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)<=3 2767, 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
43023
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 (or would HDD be better, or both?) and put that info into VB prog so the program won't work on another computer. My program uses an MSAccess table. Much appreciated if you can help! Thanks
5
2921
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 shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
7
6437
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, received.Length); string textToAdd = Encoding.ASCII.GetString(received, 0, readBytes); myCompleteMessage = String.Concat(myCompleteMessage, textToAdd); }
3
9723
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 large)? I am considering an 8K or 16K Buffer. The files sizes are random but range between 8K - 100K with the occasional files being several megs. Example: int _READBUFFER_ = 1024 ; fi = new FileInfo( args ) ;
6
2320
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 array $lineArray * @see actual_path */
3
3376
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 file aren't really useful to a user, I use the IStream::Read function together with a StreamWriter to extract single files from my compound document. When I first tested these functions everything seemed to work fine (and basically, it does),...
12
5460
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 author of one of the python database clients mentioned that using one thread to retrieve the data from the oracle database and another to insert the data into postgresql with something like a pipe between the two threads might make sense, keeping...
2
4890
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 everyone using our app has an integer encoded as 32 bits. I'm not sure what might happen should someone use a machine/OS that uses 64 bit integers, however. For instance, say that I know for certain that our server uses 32 bit integers. I can...
2
5446
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 is several GB in size. When it comes time to read the 5+GB file from inside the zip file, it fails with the following error: File "...\zipfile.py", line 491, in read bytes = self.fp.read(zinfo.compress_size) OverflowError: long it too large to...
0
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8842
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
8617
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...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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
5642
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
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1733
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.