Declaring Globally with if/switch

Is there any way to declare globally with if or switch

My problem is that I want to declare:
ifstream ScrReaderStream("D:\\file1.scrx");
globally for other 'things' to use , and for the filename to be different according to a variable, in effect the file 'number'

However ifstream only takes char as a value for the argument so I cannot include a variable within the argument.

Is there any other way to achieve what I am trying to do, or can I declare to outside of the ip/switch functions ^_^;
you can probably do it using the preprocessors #if, #else, #ifdef and #ifndef if I'm not mistaken.
sorry, but could you explain what you mean by them =/
I dont really know about preprocessors =/
Actually, you can include a variable. ifstream accepts a const char *.
You can use the open() member function
how can i make a char array or variable that can store constant,variable,constant as one 'string'? to be the directory,file number,extension
there are a few ways to do it... you can either use #define's for the constants, or just store them in a string object, then initialize a third string with the concatenation of all three:
string full_path_name( cons1 + your_var + cons2 );
then, just use the open() member of your ifstream to open the actual file.

edit: actually, after thought, it's probably easier to just use the one variable as follows since you know the directory and extension:
string full_path_name( "string literal 1" + file_variable + "string literal 2" );
Last edited on
string full_path_name( "string literal 1" + file_variable + "string literal 2" );


Actually, that might give you an error...I don' think that const char* have a + operator for std::string. You would probably want to do this:

string full_path_name( string("string literal 1") + file_variable + "string literal 2" );

I think that will work.
string full_path_name( string("string literal 1") + file_variable + "string literal 2" );
Doesnt work as a string cannot contain more than two pointers '+'

I then found that the string will only take one type of data too
I used streaming to convert the variable integer into a string and finally got a string containing all the information
however ifstream and ofstream only take const char so there is no way i can include any form of string or variable

the only way i can do this then is by declaring the if/ofstream globally in an if/switch

is there any way to do this then...?

make sure that 'file_variable' is a string object, and don't forget that
string full_path_name( string("string literal 1") + file_variable + "string literal 2" );
needs to actually be:
string full_path_name( string("string literal 1") + file_variable + string("string literal 2") );
(both the string literals need to be constructed to a string object first)
i use a static library that i wrote, and one of the functions that i wrote takes an integer and uses a stringstream to get a regular string object and returns that, if you wrote a function like that, you can use the same idea to just concatenate strings right inside the constructor for your 'full_path_name' variable.
also... remember that strings have the 'c_str()' member to get a const char* so yes, it is perfectly possible to do this.
Last edited on
Yay ^_^ sorted it now,
thanks for the help, didn't think about using the c_str method

I already had written a sequence to convert the integer into a string via streaming, and now I put it to use by using the string's c_str

Thanks again for everyones' help :3
Topic archived. No new replies allowed.