P
pillbug
Hello, I would like to know what the standard says regarding
writing beyond the end of file. For example, in the following
program, is the result always the same or is it system dependent?
#include <cstdio>
int main (int argc, char** argv)
{
char ch = 'A';
FILE* fh = NULL;
// write second byte of file
fh = std::fopen ("data.bin", "w+b");
std::fseek (fh, 1, SEEK_SET);
std::fwrite (&ch, 1, 1, fh);
std::fclose (fh);
// read first byte of file
fh = std::fopen ("data.bin", "rb");
std::fread (&ch, 1, 1, fh);
std::fclose (fh);
if (ch == 0)
std:
uts ("zero-initialized\n");
else
std:
rintf ("uninitialized: 0x%02X\n", (int) ch);
return 0;
}
I apologize for the lack of iostreams.
writing beyond the end of file. For example, in the following
program, is the result always the same or is it system dependent?
#include <cstdio>
int main (int argc, char** argv)
{
char ch = 'A';
FILE* fh = NULL;
// write second byte of file
fh = std::fopen ("data.bin", "w+b");
std::fseek (fh, 1, SEEK_SET);
std::fwrite (&ch, 1, 1, fh);
std::fclose (fh);
// read first byte of file
fh = std::fopen ("data.bin", "rb");
std::fread (&ch, 1, 1, fh);
std::fclose (fh);
if (ch == 0)
std:
else
std:
return 0;
}
I apologize for the lack of iostreams.