Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > get stream mode flags from an opened stream

Reply
Thread Tools

get stream mode flags from an opened stream

 
 
Alexander Korsunsky
Guest
Posts: n/a
 
      02-17-2007
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-17-2007
Alexander Korsunsky wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Quetion about flags of socket.recv(bufsize, [flags]) Steve Holden Python 0 02-08-2009 04:09 PM
re: mmm-mode, python-mode and doctest-mode? Edward Loper Python 0 08-07-2007 08:58 AM
mmm-mode, python-mode and doctest-mode? John J Lee Python 3 12-01-2005 08:35 PM
Accidentaly opened I-Bagle - and then opened virus vault ?? Morph Computer Information 2 02-01-2005 03:43 AM
Safe Mode (?) - It is meant to be normal mode but looks like safe mode English Patient Computer Support 3 10-03-2004 11:10 PM



Advertisments