By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,409 Members | 903 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,409 IT Pros & Developers. It's quick & easy.

writing beyond EOF and uninitialized bytes

P: n/a
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::puts ("zero-initialized\n");
else
std::printf ("uninitialized: 0x%02X\n", (int) ch);

return 0;
}

I apologize for the lack of iostreams.
Mar 9 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
pillbug wrote:
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::puts ("zero-initialized\n");
else
std::printf ("uninitialized: 0x%02X\n", (int) ch);

return 0;
}

I apologize for the lack of iostreams.
Try it on different platforms and see it for youself. You could
propbably assume constant behavior.
Mar 9 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.