Writing to a binary file.

Posted on 2006-07-09
Last Modified: 2010-04-15
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.

Question by:jewee
  • 2
  • 2
  • 2
  • +3
LVL 45

Expert Comment

ID: 17070541
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.

LVL 12

Expert Comment

ID: 17070570
Post the code !
LVL 24

Expert Comment

ID: 17070695
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.


Author Comment

ID: 17070698
This is not a homework assignment - i will show you the code that I put together so far.
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!


Expert Comment

ID: 17071478

 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


Author Comment

ID: 17083380

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);
LVL 14

Expert Comment

ID: 17085365
> 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.

Expert Comment

ID: 17090410

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
LVL 45

Accepted Solution

sunnycoder earned 500 total points
ID: 17154647
Thanks NM ...

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

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c language help - file paths 7 141
c++ substatte a varabe for a string in a LPCTSTR statment 8 83
Arduino EDI - Programming language 3 80
Super Scope, DHCP 5 52
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now