Reputation: 3657
I'm reading double
data in from a DAQ that has 2 channels. Data is stored in read_buf
, the first 1000
samples are channel 0
and the second 1000
are channel 1
. I have no control over this concatenation of channel data.
I have set up 2 files like so,
FILE *fptr_0;
if ((fptr_0 = fopen("channel_0.bin", "wb")) == NULL)
{
printf("Error opening file.\n");
exit(1);
}
FILE *fptr_1;
if ((fptr_1 = fopen("channel_1.bin", "wb")) == NULL)
{
printf("Error opening file.\n");
exit(1);
}
I would then like to split read_buf
and send the first half to fptr_0
and the second to fptr_1
. I can read out the first half but am flummoxed on how to read out the second half. How do I point fptr_1
to just the second half of read_buf
?
Do I have to copy each half into a new array?
What I have so far which works for the first half followed by what I'm not getting,
double read_buf[2000];
result = DAQ_func(device, &status, read_buf);
fwrite(&read_buf, sizeof(double), (sizeof(read_buf) / sizeof(read_buf[0])) / 2, fptr_0);
fwrite( ??? , sizeof(double), (sizeof(read_buf) / sizeof(read_buf[0])) / 2, fptr_1);
Upvotes: 0
Views: 92
Reputation: 7345
How do I point fptr_1 to just the second half of read_buf?
You do not want to point the FILE *
to anything but the stream associated with the file.
fwrite( ??? , sizeof(double), (sizeof(read_buf) / sizeof(read_buf[0])) / 2, fptr_1);
Adding 1000 to read_buf
will increment it by the sizeof it's contents.
So that gives us:
fwrite (read_buf + 1000, sizeof(double), (sizeof(read_buf) / sizeof(read_buf[0])) / 2, fptr_1);
Or:
fwrite (&read_buf[1000], sizeof(double), (sizeof(read_buf) / sizeof(read_buf[0])) / 2, fptr_1);
where &read_buf[1000]
will yield a pointer to the 1001th element of the array.
Is this the most efficient way? Perhaps not, but it's correct.
The return value of fwrite()
should be checked too:
The fwrite() function shall return the number of elements successfully written, which may be less than nitems if a write error is encountered. If size or nitems is 0, fwrite() shall return 0 and the state of the stream remains unchanged. Otherwise, if a write error occurs, the error indicator for the stream shall be set, and errno shall be set to indicate the error.
Upvotes: 1
Reputation: 6573
???
is read_buf + 1000
(no &).
fwrite(read_buf + 1000, sizeof (double), (sizeof read_buf / sizeof read_buf[0]) / 2, fptr_1);
Or if you prefer &read_buf[1000]
which is the same.
Upvotes: 2