Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overhead data when writing to a binary file

Tags:

c

I want to write a array containing 16bit integers as raw binary to a file and have tried with the following example:

#  define __int8_t_defined
__intN_t (8, __QI__);
__intN_t (16, __HI__);
__intN_t (32, __SI__);
__intN_t (64, __DI__);

int main(int argc, char *argv[])
{
    FILE * rawf;
    rawf = fopen("./rawPcm","wb");
    int16_t buff[] = {0,0,0};
    fwrite(buff,sizeof(int16_t), sizeof(buff),rawf);
    fwrite(buff,sizeof(int16_t), sizeof(buff),rawf);  
    fclose(rawf);
}

However, the output contains more than just zeros.

$ hexdump -v rawPcm 
0000000 0000 0000 0000 85fd 0804 0001 0000 0000
0000010 0000 85fd 0804 0001                    
0000018

It writes 0000 0000 0000 85fd 0804 0001 for every fwrite(buff,sizeof(int16_t), sizeof(buff),rawf); while I would expect to get only 0000 0000 0000.

What does the additional data represent 85fd 0804 0001 and how do I prevent it from occurring?

like image 687
TheMeaningfulEngineer Avatar asked Jul 22 '15 07:07

TheMeaningfulEngineer


People also ask

How do I write a text file to a binary file?

Now using fwrite (&cs, rec_size, 1, fp_output), write one cs object of size rec_size to the binary file- ” fp_output” (binary mode). This procedure continues for all the lines of text files until the end of the file is reached.

How to create a binary file named employee and student?

To create a binary file named employee.dat and write details of employees available in the form of dictionaries. To create a binary file named student.dat . Data of students must be put at run time then it should be stored in file. The structure of data is (rollno, name, marks).

How to write an object to a binary file in Python?

You must first complete Working with binary files in Python before viewing this Lesson To write an object to a binary file opened in the write mode, we should use dump ( ) function of pickle module as per the following syntax : For example, if you have a file open in handle f1 as f1=open (“file1.txt”,”wb”) 1.

How do I write a structure variable to a binary file?

Write that structure variable to the output binary file using fwrite (). Close the input text file and the output binary file. Below is the C++ program for the above approach:


2 Answers

What does the additional data represent 85fd 0804 0001

Possibly some random garbage data.

how do I prevent it from occurring?

fwrite(buff,sizeof(int16_t), sizeof(buff),rawf); should be written as:

fwrite(buff,sizeof(int16_t), sizeof(buff) / sizeof(buff[0]),rawf);
/*               ^                          ^^^^^^^^^^^^^^^      */
/*       size of each object      Count of objects               */
/*              (2)                  (3)                         */

/* or */
fwrite(buff, sizeof buf[0], sizeof buff / sizeof buff[0], rawf);

sizeof buff / sizeof buff[0] gets the array length in number of objects (or members) while sizeof buff gives the size of array in bytes.

So you are reading past the buff and writing it to the file and invoking undefined behaviour. In your case you are seeing the random garbage data being written to output file.

In your case, sizeof of each element in buff is 2 bytes and buff array has 3 members causing total size to be 6 bytes. When you write fwrite(buff,sizeof(int16_t), sizeof(buff),rawf);, 6 objects of 2 byte each are written to the file which is not something that you want to do.

Now you are writing 6 data of type (size) int16_t starting from buff[0] i.e. buff[0..5] to the output. buff[0..2] are 0s as expected and buff[3..5] are garbage.

like image 95
Mohit Jain Avatar answered Oct 22 '22 17:10

Mohit Jain


The third argument of fwrite is the number of elements of size specified by the second parameter. It is not the total size in bytes, which you are currently doing.

So you are overrunning your buffer.

like image 25
Bathsheba Avatar answered Oct 22 '22 19:10

Bathsheba