get stream mode flags from an opened stream

  • Thread starter Alexander Korsunsky
  • Start date
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::eek: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::eek: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::eek: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::eek: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
 
J

John Harrison

Alexander said:
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.

No, is the short answer. I guess the reasoning is why would you care?
I did not find any functions in C, but I found something similar in C++,
the flags() member functio of an fstream object.

flags() returns formatting flags, not mode flags. They're different
things, formatting flags are things like the current radix for integer
output and the current justification.

[snip]
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?

I think the better way would be to write your own file class. Something
like this

class SpecialFile
{
public:
SpecialFile(const char* name)
{
// ensure that _file is opened correctly
...
}
private:
FILE* _file;
};

class OtherClass
{
public:
void some_method(SpecialFile& file);
};

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top