[ back to toc ]

file read/write

Date: 2002/06/17 15:16

Q:
Hi!

I created an array called buff_ptr[MAX_SIZE] that is initially filled with
all zeros.

I fread all of the data from a binary file and put it in the buff_ptr[].

Now I want to write all of the data with about 50 zeros between the data.
So, the outfile should look like this:
- 1st half of data
- 50 zeros
- 2nd half of data

I have written the 1st half of the data using the following:
fwrite(&buff_ptr[i], 1, 1, out_file_stream)

How do I go about writing 50 zeros? Note: When writing 50 zeros, isn't the
file ptr moving; thus the file ptr to pointing somewhere in the 2nd half
of the data. How do I go to the start of the 2nd half of data and write
the 2nd half of the data out after the zeros.

Hope I explained it clearly!
thanks, jc
A:
First of all

fwrite(&buff_ptr[i], 1, 1, out_file_stream)

could be written as

fwrite(buff_ptr+i, 1, 1, out_file_stream)

But why do it in a loop instead of

fwrite(buff_ptr, 1, NumberOfBytes, out_file_stream)

??

>>>
Note: When writing 50 zeros, isn't the file ptr moving; thus the file ptr
to pointing somewhere in the 2nd half of the data.
<<<

I do not get the point. When you write 50 zero characters to the file then
the filepointer is advancing 50 places ahead. For example:

char zeros[50];

memset(zeros,50,0);
fwrite(zeros,1,50,out_file_stream);

writes out 50 zero characters (may be memset args I mixed up, see man
page). After that

fwrite(buff_ptr, 1, NumberOfBytesOfSecondHalf, out_file_stream)

should just work fine.

regards,
Peter

[ back to toc ]