Write a binary file containing zeros to a Linux file system

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Write a binary file containing zeros to a Linux file system



I have a large block of memory in my C program that contains zeros in the middle. I want to save this to disk without converting the values to ASCII. Will writing zeros to a file stream cause a false-negative EOF when I try to read from that file later?



Example:


int x[1024] = {1, 2, 3, 4, 0, 0, 0, 0, 3};
// write x to disk in myfile.bin



Thanks in advance!





Not if you use fwrite to write the memory block to a binary file. Next, 0 is not an EOF indicator. Except in some old text file formats (which used 0x1A), there is no EOF indicator present as part of the file.
– Weather Vane
6 mins ago




fwrite


0


0x1A




1 Answer
1



Null bytes in a file don't trigger EOF, even on text files. Hitting EOF is something the I/O libraries are able to detect regardless of content.



As long as you use write or fwrite to write the memory block to disk and later use read or fread to read it, you should be fine.


write


fwrite


read


fread






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived