Test whether iostream is binary or plain text
-
I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.
class MyClass {
friend ostream& operator <<(ostream& stream, const MyClass& obj);
friend istream& operator >>(istream& stream, MyClass& obj);
}
ostream& operator <<(ostream& stream, const MyClass& obj) {
// How do I do the following?
if ([stream is opened for binary writes]) {
...
} else {
...
}
}
istream& operator >>(istream& stream, MyClass& obj) {
// How do I do the following?
if ([stream is opened for binary reads]) {
...
} else {
...
}
}Would doing such a thing be poor design, or is this pretty acceptable? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.
class MyClass {
friend ostream& operator <<(ostream& stream, const MyClass& obj);
friend istream& operator >>(istream& stream, MyClass& obj);
}
ostream& operator <<(ostream& stream, const MyClass& obj) {
// How do I do the following?
if ([stream is opened for binary writes]) {
...
} else {
...
}
}
istream& operator >>(istream& stream, MyClass& obj) {
// How do I do the following?
if ([stream is opened for binary reads]) {
...
} else {
...
}
}Would doing such a thing be poor design, or is this pretty acceptable? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
First: A stream is not binary or plain text, it is: do i want to treat incoming bytes as binary or as plain text. It is perfectly legal to open a stream containing plain text as binary. Next: you must open a stream with a flag that tells how you want to open it, so you already know if it's binary or not. Ergo: there's no need to test this. But i guess i'm completely missing the point...
-
First: A stream is not binary or plain text, it is: do i want to treat incoming bytes as binary or as plain text. It is perfectly legal to open a stream containing plain text as binary. Next: you must open a stream with a flag that tells how you want to open it, so you already know if it's binary or not. Ergo: there's no need to test this. But i guess i'm completely missing the point...
Ok, let me rephrase... one of the members of MyClass is an array. If the user wants to output my class in a human-readable form, I want to print the array as "[elem1, elem2, ... elemN]". If not, then I want to output the array length followed by each element in a binary representation. Now, when someone tries to output my class as "cout << MyClassInstance", it should be the human-readable version. When serializing the class to a file for later reloads, I want to output the binary version. How can I infer which version to use? I could conceivably write a specific implementation for ifstream and ofstream so I always read/write the non-human-readable version to a file, but that isn't really what I am trying to do. I could also write a specific method to serialize vs. just outputting my class, but again, I don't know what the standard way of doing this is. Thanks for any insight,
Sounds like somebody's got a case of the Mondays -Jeff
-
I would like to test whether a stream was opened for binary read/writes or for plain text. The goal of my code is to output the data in a MyClass object in two different ways depending on this stream property.
class MyClass {
friend ostream& operator <<(ostream& stream, const MyClass& obj);
friend istream& operator >>(istream& stream, MyClass& obj);
}
ostream& operator <<(ostream& stream, const MyClass& obj) {
// How do I do the following?
if ([stream is opened for binary writes]) {
...
} else {
...
}
}
istream& operator >>(istream& stream, MyClass& obj) {
// How do I do the following?
if ([stream is opened for binary reads]) {
...
} else {
...
}
}Would doing such a thing be poor design, or is this pretty acceptable? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
std::ios_base::open_mode can tell you how the file stream was opened. The values are listed in xiosbase.h [edit] in VS.
modified on Monday, February 1, 2010 8:42 AM
After reading your post, I assumed that I could just use the statement:
bool const isBinary = ((out.flags() & ios_base::binary) != 0);
However, after testing this it appears that the ios_base::open_mode is not stored in the return value of "ostream::flags()". I have been unable to figure out how to get something from an ostream object that tells me the ios_base::open_mode used. Any idea how to extract this information after the file has been opened? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
-
After reading your post, I assumed that I could just use the statement:
bool const isBinary = ((out.flags() & ios_base::binary) != 0);
However, after testing this it appears that the ios_base::open_mode is not stored in the return value of "ostream::flags()". I have been unable to figure out how to get something from an ostream object that tells me the ios_base::open_mode used. Any idea how to extract this information after the file has been opened? Thanks,
Sounds like somebody's got a case of the Mondays -Jeff
Wow. 9 months later. Internet been a bit slow? I'm sorry, I also incorrectly assumed the mode would be accessible somewhere within the stream obj. Other than scanning the array data for CR,LF combos, or trying different read/write modes and seeing what fails and what succeeds, I don't how to determine how the stream was opened.
-
Wow. 9 months later. Internet been a bit slow? I'm sorry, I also incorrectly assumed the mode would be accessible somewhere within the stream obj. Other than scanning the array data for CR,LF combos, or trying different read/write modes and seeing what fails and what succeeds, I don't how to determine how the stream was opened.
Yeah, clearly I get sidetracked easily. Anyway, I ultimately determined that opening a file in binary mode doesn't really do what I thought it did... I was under the impression that doing so would force the shift operator to output data in binary, which it doesn't. Therefore, I decided to implement two methods: one to output the data in binary, and the other to output it in ASCII format. Thanks for the help, though.
Sounds like somebody's got a case of the Mondays -Jeff