get stream mode flags from an opened stream

Discussion in 'C++' started by Alexander Korsunsky, Feb 17, 2007.

  1. 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
    Alexander Korsunsky, Feb 17, 2007
    #1
    1. Advertising

  2. 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
    John Harrison, Feb 17, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John J Lee
    Replies:
    3
    Views:
    470
    bruno at modulix
    Dec 1, 2005
  2. Edward Loper
    Replies:
    0
    Views:
    457
    Edward Loper
    Aug 7, 2007
  3. John J Lee
    Replies:
    0
    Views:
    511
    John J Lee
    Aug 7, 2007
  4. Edward Loper

    mmm-mode, python-mode and doctest-mode?

    Edward Loper, Aug 9, 2007, in forum: Python
    Replies:
    0
    Views:
    424
    Edward Loper
    Aug 9, 2007
  5. Steve Holden
    Replies:
    0
    Views:
    772
    Steve Holden
    Feb 8, 2009
Loading...

Share This Page