accessing fstream open arguments

Hi -

I'd like to write a utility routine to open files. One of the arguments to the routine would be the io mode (read, write, etc.) How does one access these arguments and store them in a variable?

This is what I'd like to do:

1
2
3
some_variable_type mode;
mode = ios::out | ios:: ate;
rc = openFile (fileName, mode, fp);


Thanks...
My immediate instinct would be to simply create your own determinant, and then make an if-else statement to decide which mode you're going to use.
Yeah, I can do that easily enough; I was just wondering whether there was a more direct route. But, that's fine. Thanks.
I know there are languages out there that can use and modify their own commands and variable names, but I don't think that C++ is one of them.
I'm not really trying to modify anything; just access them somehow. For example, I know that ios::out is a 0x10, but I don't know how to "grab" that value and pass it as a parameter (or even use it in a variable as I described above). It doesn't make sense that C++ would keep that hidden from the user, does it?
Well, what you're really asking to do is to pass a non-variable command type argument to a function, which I don't think I've ever seen done. In order for you to be able to do that, C++ as a language would have to be aware of itself, which it isn't.
¿ std::ios_base::openmode ?
¿Or do you want to implement something like http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.20 ?
ne555: that's exactly what I wanted. My routine now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template <class T> void	openFile(string& name,
			 T& file,
			 const	int	ioMode)		// 0 = read; 1 = write
{
	std::ios_base::openmode ioFlags;

	if (ioMode == FILE_READ)
		ioFlags = ios_base::in;
	else
		ioFlags = ios_base::out;

	file.setf(ios::hex, ios::basefield);
	file.open(name.c_str(), ioFlags);
	if (!file.good())
	{
		cout << "Demod: error opening " << name << "." << endl;
	}
}


This seems to work fine; thanks for the help.
How about:
#define mode (ios::out | ios:: ate)
Hi, Stewbond - the program I'm running should scrap all old output and regenerate the files, so the ate bit isn't appropriate here. I'll keep it in mind for future applications, though.
Topic archived. No new replies allowed.