Re: How to read data from several files into a single buffer?
- From: Skarmander <invalid@xxxxxxxxxxxxxx>
- Date: Mon, 31 Oct 2005 20:06:59 +0100
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 you're just signaling generic failure, use the portable EXIT_FAILURE, not 1.if (mydata == NULL) { perror("malloc - mydata"); exit(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. .
- References:
- Prev by Date: Re: Why this code is working
- Previous by thread: How to read data from several files into a single buffer?
- Index(es):