Solved

Writing to a binary file.

Posted on 2006-07-09
12
381 Views
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.

0
Comment
Question by:jewee
  • 2
  • 2
  • 2
  • +3
12 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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.

Cheers!
sunnycoder
0
 
LVL 12

Expert Comment

by:rajeev_devin
Comment Utility
Post the code !
0
 
LVL 24

Expert Comment

by:fridom
Comment Utility
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.

Regards
Friedrich
0
 

Author Comment

by:jewee
Comment Utility
This is not a homework assignment - i will show you the code that I put together so far.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Expert Comment

by:srinims
Comment Utility
hi,

 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;

memcpy(buffer2,s1,sizeof(s1));

//u can use the old size to track the buffer2 pointer
memcpy(buffer2+sizeof(s1),buffer1,48);
memcpy(buffer2+sizeof(s1)+48,0,1);


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;
memset(buffer2,0,maxBytes);
for(; (check the size of buffer1 with multiples of 48);)
{
       
        memcpy(buffer2+buffer2pos,s1,sizeof(s1));
        buffer2pos+=sizeof(s1);        
        memcpy(buffer2+buffer2pos,buffer1,48);
        buffer2pos+=48;
//        memcpy(buffer2+buffer2pos,0,1);
        buffer2pos+=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

0
 

Author Comment

by:jewee
Comment Utility

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);
    free(filebuffer);
}
0
 
LVL 14

Expert Comment

by:cwwkie
Comment Utility
> 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.
    filebufferpos=0;
    memcpy(filebuffer+filebufferpos, teststruct, sizeof(teststruct));
    filebufferpos+=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.
0
 
LVL 3

Expert Comment

by:srinims
Comment Utility
use

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

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
Thanks NM ...

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 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

11 Experts available now in Live!

Get 1:1 Help Now