A
Alexander Korsunsky
Hi!
Is it possible to extract the mode flags of a stream (FILE* stream or
fstream - object), without looking at how the stream was opened?
What I mean by mode flags is wether the stream is opened in read only
mode, write only, binary and so on.
I did not find any functions in C, but I found something similar in C++,
the flags() member functio of an fstream object.
I've tried to check the flags on ios_base::binary ios_base::in binary
ios_base:ut, but no matter how the stream was opened, it always
returns ios_base::ate. That's the code:
std::fstream file;
// It doesnt really matter how I open it, the flags are the same
mp3_file.open(argv[1], std::ios::in | std::ios:ut );
if (!file)
{ std::cout<<"An error occured \n"; return 0; }
if (!file.good())
{ std::cout<<"An error occured \n"; return 0; }
std::ios_base:penmode flags;
flags = file.flags();
// This prints allways 1002
printf ("Flags: %x\n", flags);
if ( flags & std::ios_base::in )
printf ("This stream is opened with the flag in.\n");
if ( flags & std::ios_base:ut )
printf ("This stream is opened with the flag out.\n");
if ( flags & std::ios_base::trunc )
printf ("This stream is opened with the flag trunc.\n");
if ( flags & std::ios_base::app )
printf ("This stream is opened with the flag app.\n");
// I'm always getting this one
if ( flags & std::ios_base::ate )
printf ("This stream is opened with the flag ate.\n");
Then I had a closer look at the member function and saw that it was
mainly used to determine the flags for an istream/ostream object like
cin and cout, am I correct? That's the link:
http://www.cplusplus.com/reference/iostream/fstream/
So, is there any way to achieve what I want? Also, is there a way to do
it in C ?
The backround of this question is that I've written a class, and a few
member functions expects a stream as FILE* pointer as argument. But this
stream has to be opened in a special way, otherwise the file associated
will be corrupted. The only way to prevent this now is to threaten the
user of the class to open the streams in the correct mode, but I think
there must be a better way.
Does anybody know how to manage that?
Thanks in Advance,
Alexander Korsunsky
Is it possible to extract the mode flags of a stream (FILE* stream or
fstream - object), without looking at how the stream was opened?
What I mean by mode flags is wether the stream is opened in read only
mode, write only, binary and so on.
I did not find any functions in C, but I found something similar in C++,
the flags() member functio of an fstream object.
I've tried to check the flags on ios_base::binary ios_base::in binary
ios_base:ut, but no matter how the stream was opened, it always
returns ios_base::ate. That's the code:
std::fstream file;
// It doesnt really matter how I open it, the flags are the same
mp3_file.open(argv[1], std::ios::in | std::ios:ut );
if (!file)
{ std::cout<<"An error occured \n"; return 0; }
if (!file.good())
{ std::cout<<"An error occured \n"; return 0; }
std::ios_base:penmode flags;
flags = file.flags();
// This prints allways 1002
printf ("Flags: %x\n", flags);
if ( flags & std::ios_base::in )
printf ("This stream is opened with the flag in.\n");
if ( flags & std::ios_base:ut )
printf ("This stream is opened with the flag out.\n");
if ( flags & std::ios_base::trunc )
printf ("This stream is opened with the flag trunc.\n");
if ( flags & std::ios_base::app )
printf ("This stream is opened with the flag app.\n");
// I'm always getting this one
if ( flags & std::ios_base::ate )
printf ("This stream is opened with the flag ate.\n");
Then I had a closer look at the member function and saw that it was
mainly used to determine the flags for an istream/ostream object like
cin and cout, am I correct? That's the link:
http://www.cplusplus.com/reference/iostream/fstream/
So, is there any way to achieve what I want? Also, is there a way to do
it in C ?
The backround of this question is that I've written a class, and a few
member functions expects a stream as FILE* pointer as argument. But this
stream has to be opened in a special way, otherwise the file associated
will be corrupted. The only way to prevent this now is to threaten the
user of the class to open the streams in the correct mode, but I think
there must be a better way.
Does anybody know how to manage that?
Thanks in Advance,
Alexander Korsunsky