• C

Writing to a binary file.

Hello experts - I am having problems with keeping track of the positioning of buffer2!  

This is what I am trying to do...
I am trying to write to a binary file.   I have created a struct (size 12 bytes).

genData(teststruct &test1, unsigned char *buffer1, int maxBytes)
   //Allocate memory for buffer2
   unsigned char buffer2* = (unsigned char*) malloc (maxBytes);    
   Copy struct into buffer2
   Then read the first 48 bytes from buffer1 and append it to buffer2
   Append arbitrary 1 byte value to buffer2

   Append same struct to buffer2
   Read in the next 48 bytes from buffer1 and append it to buffer2
   Append 1 byte value to buffer2...

etc...until all bytes have been used from buffer1.
Once all bytes have been used - write entire buffer2 to file.

Who is Participating?
sunnycoderConnect With a Mentor Commented:
Thanks NM ...

jewee - any progress on this? Were you able to get it working?
Hi jewee,

This soulds like a course assignement ...
> I am having problems with keeping track of the positioning of buffer2!
Can you show us the code ... we will be glad to help you get it working.

Post the code !
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

You  buffers are arrays so you access the position the usual way.

char buf[SOME_SIZE]

buf[1] = .....

buf[48] = ....

what you are trying to achieve is "flattening"  structure. This is e.g the way data are saved in Berkeley DB. So check the examples and you might get an idea.

jeweeAuthor Commented:
This is not a homework assignment - i will show you the code that I put together so far.

 u can use memcpy to copy data from char buffer to structure and u can use the same to append also.

struct S s1;
char * buffer1;
char * buffer2;


//u can use the old size to track the buffer2 pointer

the code given won't work directly u have to change covert the type in to void*,char* etc.

you have to repeat the code n a while or for loop, by checking the size of buffer1 with (number of times of loop)*48.

in place of 48 you have to 48*i, i is the increment value in for loop.
and in the first memcpy you have to use the old size;

better do

int buffer2pos=0;
for(; (check the size of buffer1 with multiples of 48);)
//        memcpy(buffer2+buffer2pos,0,1);

track the buffer pos of buffer2 using buffer2pos.

do the other checking with yourself.
if u find any difficulties we will help you.

u can use the strlen to find the length of buffer1. and then you can divide the length with 48. and you can run the loop for the strlen(b1)/48 times

jeweeAuthor Commented:

I am trying to do something like this.
unsigned char test = 0x01;
int numOfBytes = 0;
memset(filebuffer, 0, maxBytes)
while (numOfBytes < = maxBytes)
    //Write struct to buffer
    numOfBytes +=48;
    memcpy(filebuffer, teststruct, sizeof(teststruct));
    memcpy(filebuffer, buffer1+numOfBytes, 48);
    memcpy(filebuffer, test, 1);
    maxBytes -= 48;
    fwrite(&filebuffer, sizeof(filebuffer), 1, fp);
> This is not a homework assignment
Then I asume you can create the function yourself, I only explain what you need to do.

memcpy always writes to the start of the destination, so if you want to append data, you need to remember the current position, eg.
    memcpy(filebuffer+filebufferpos, teststruct, sizeof(teststruct));

>    fwrite(&filebuffer, sizeof(filebuffer), 1, fp);
>    free(filebuffer);
Here is something wrong. Either filebuffer is an array, so you can use sizeof, or it is a pointer, then you can use free. The combination is not possible.

while(numOfBytes < = maxBytes && buffer1+numOfBytes < strlen(buffer1) )

and write the file buffer outside the loop

use numberOfBytes in place of sizeof(filebuffer);

use my code mentioned above. your code has some errors
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.