473,788 Members | 3,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::binar y 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(a rgv[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 3860
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(con st char* name)
{
// ensure that _file is opened correctly
...
}
private:
FILE* _file;
};

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

john
Feb 17 '07 #2

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

Similar topics

3
2013
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
5098
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 there any way to determine, after calling the open(filename,ios::app) method, which of these two alternatives has occurred? I would like to differentiate the program operation, depending
4
2212
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 additinional methods to deal with it, i.e. open/close/write: classA { private: const std::auto_ptr<std::ofstream> filePtr;
9
17968
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 FileNum = FreeFile() FileOpen(FileNum, TextBox1.Text, OpenMode.Random, , , Len(Employee)) Count = 1 lstView.Items.Clear() Do While Not EOF(FileNum) FileGet(FileNum, Employee, Count)
2
2738
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
2041
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 the problem where a file has been opened as text and then is used on a Windows system as Windows 'helpfully' inserts extra characters when it sees some control characters. Whilst there is a mechanism to obtain format flags I have been
3
3607
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 want it to do is read the second line of the file, however, for some reason, it grabs the third line. I get similar results with fgetpos and fsetpos. Is there something simple I'm missing here? My code is below: /
20
5116
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 snip in question: $file = "time";
2
2485
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 Mining\public_html\lib\inc\functions.lib.php Line 673 : trigger_error("Failed to open stream to $filename. Permission denied!", E_USER_ERROR); could you plz tell the solution for how to avoid that errors
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.