Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing to a binary file.

Posted on 2006-07-09
12
Medium Priority
?
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
12 Comments
 
LVL 45

Expert Comment

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

Cheers!
sunnycoder
0
 
LVL 12

Expert Comment

by:rajeev_devin
ID: 17070570
Post the code !
0
 
LVL 24

Expert Comment

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

Regards
Friedrich
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:jewee
ID: 17070698
This is not a homework assignment - i will show you the code that I put together so far.
0
 
LVL 3

Expert Comment

by:srinims
ID: 17071478
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
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);
    free(filebuffer);
}
0
 
LVL 14

Expert Comment

by:cwwkie
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.
    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
ID: 17090410
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 2000 total points
ID: 17154647
Thanks NM ...

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.
Suggested Courses

705 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