473,396 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

get stream mode flags from an opened stream

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::out, 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::out );

if (!file)
{ std::cout<<"An error occured \n"; return 0; }

if (!file.good())
{ std::cout<<"An error occured \n"; return 0; }

std::ios_base::openmode 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::out )
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
Feb 17 '07 #1
1 3817
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
Feb 17 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gernot Frisch | last post by:
does this work: struct XY { int crc; std::string filename; } std::ofstream& operator<<(std::ofstream& os, const XY& me) { os << me.crc;
14
by: Leslaw Bieniasz | last post by:
Cracow, 3.01.2005 Hello, When opening a file stream in the append mode, either a new file is created (if a specified file does not exist), or an existing file is opened for adding stuff. Is...
4
by: Anton Pervukhin | last post by:
Hi everybody! I have a small problem regarding the initialization of pointer to the file stream under some conditions. Imagine a class which has a pointer to output file stream and some...
9
by: Zak Milas | last post by:
I have the code below: Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click Dim Employee As UploadFile, Count As Integer, Temp As String...
2
by: kelvSYC | last post by:
Suppose I have something like this: class foo { std::istream in; public: foo(std::istream& in_) : in(in_.rdbuf()); void doStuffWith(std::ostream& out); void bar(); };
8
by: Aaron Turner | last post by:
Dear All, I am working on some cross-platform code and using read() and write() on binary streams. What I would like to do is to determine whether a stream has been opened as binary to avoid...
3
by: dfelikson | last post by:
I have a simple piece of code that ftell's me the position after reading the first line of a file, then reads two more lines, then fseek's back to the position, and reads the next line. What I...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
2
by: swethak | last post by:
Hi, when i run my code it gives error as fopen(lib/providers//provider.RVLogic.php): failed to open stream: Permission denied in F:\Facebook\furniture11\Data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.