Discussion:
std::ofstream() problem : not failing on malformed filenames, just not writing anything.
(too old to reply)
R.Wieser
2013-01-01 13:50:27 UTC
Permalink
Hello All,

My first steps with VC8 express (Win XP), and I'm compiling a open-source
game called BZFlag.

Somewhere in there here is a

std::ofstream(filename.c_str(), mode);

command, and normally it works well. The the filename is comprized outof a
predefined path (into an existing folder), a name part provided by the user
and a fixed extension.

But users are downright silly beings, not knowing that, for example, a colon
":" is not a valid character in a (windows filesystem) filename. So they
sometimes use them.

The problem is that as a programmer I expected that the "std::ofstream"
function would barf on it so the user could be told to cleanup its filename.
Alas, nothing of the sort.
when I provide a filename like "C:\\foo:bar.tmp" a file named "foo" is
created, but its always zero bytes.

I've tried looking at several status flags (good(), bad() , is_open(), etc),
but none of them seem to, in the above case, indicate anything amiss.

[TL;DR - start reading here]
So, the question is : how does std::ofstream tell me (if at all) that the
above failed and/or how do I detect nothing got written to the file (did
look for but could not find a function (somehow) returning the ammount of
data written into the targetted file).

Regards,
R.Wieser

P.s.
Although I have programming experience, I'm a newbie in regard to "Visual
C++ 2008 Express edition" and the used language therein ...
R.Wieser
2013-01-01 20:47:50 UTC
Permalink
Hello All,

The described problem was located deeper than I thought: I replicated the
behaviour using the basic "CreateFile" function (Kernel32).

The answer ? The "alternate data stream" (ADS), which only works on NTFS
volumes. Its a fully expected behaviour, just not a really known one. :-)

Thanks to Jeroen Mostert for providing me the answer to it (in
comp.os.ms-windows.programmer.win32).

Regards,
Rudy Wieser
Post by R.Wieser
Hello All,
My first steps with VC8 express (Win XP), and I'm compiling a open-source
game called BZFlag.
Somewhere in there here is a
std::ofstream(filename.c_str(), mode);
command, and normally it works well. The the filename is comprized outof a
predefined path (into an existing folder), a name part provided by the user
and a fixed extension.
But users are downright silly beings, not knowing that, for example, a colon
":" is not a valid character in a (windows filesystem) filename. So they
sometimes use them.
The problem is that as a programmer I expected that the "std::ofstream"
function would barf on it so the user could be told to cleanup its filename.
Alas, nothing of the sort.
when I provide a filename like "C:\\foo:bar.tmp" a file named "foo" is
created, but its always zero bytes.
I've tried looking at several status flags (good(), bad() , is_open(), etc),
but none of them seem to, in the above case, indicate anything amiss.
[TL;DR - start reading here]
So, the question is : how does std::ofstream tell me (if at all) that the
above failed and/or how do I detect nothing got written to the file (did
look for but could not find a function (somehow) returning the ammount of
data written into the targetted file).
Regards,
R.Wieser
P.s.
Although I have programming experience, I'm a newbie in regard to "Visual
C++ 2008 Express edition" and the used language therein ...
Loading...