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!
fwriteto write the memory block to a binary file. Next,0is not an EOF indicator. Except in some old text file formats (which used0x1A), there is no EOF indicator present as part of the file.fwrite()to write the data up to the start of the (next) sequence of zeros, then usefseek(handle, N*sizeof (int), SEEK_CUR)to "write"Nzeros, assuming this is a new file, not overwriting old file data). When read, the unwritten data will read as zeros, exactly as if you had written the zeros. Such files are called "sparse", and are relatively common in Linux, Unix, and POSIXy systems.