How i can have global outputFile with local value?

Hello, i have fileforimport and outputFile globals, but the a must be local.
Can it happend with some way?


global
char fileforimport [16];


locals
scanf ("%s",fileforimport);
a=fileforimport+"jjj";


global
ofstream outputFile(a.c_str(),ios::binary);

what?
why don't you do something like:

global
string file_for_import;

locals
char temp[20];
scanf("%s",temp);
file_for_import=temp;
file_for_import+="jjj";

global
ofstream outputFile(file_for_import.c_str(),ios::binary);

but it would be better if you told us what exactly is it that you want to do...
Last edited on
In outputFile write many modules. So outputFile must be global. But the name of outputFile consist from different parts. So in main function file_for_import must be constructing.
Then how can i have global outputFile?
Ah, you see, when you have an ofstream object it's not necessary to associate a file with it upon instantiation.

Just declare your ofstream object in global scope like:
ofstream outputFile;

Then you can later do something like:
outputFile.open(file_for_import.c_str(),ios::binary);
(and file_for_import can be local)

And when you finish with it do something like:
outputFile.close(); and then open another file.
Last edited on
Ok, i just open my pc. I will try with this piece of code.
Many thanh's, and good day.
Jim.
use variable static
may be can help
Topic archived. No new replies allowed.