I have the following problem. I make a program, part of which saves an array to a .dat file. So I created a function called writetofile, which does this using fopen. But the problem is, I want the name of the file to be read off from the name of the variable passed to the function. In other words, if I write
writetofile(chi_SOR,...)
I want this to be saved to a file called chi_SOR.dat, and if I call
writetofile(chi_ADI,...)
I want this to be saved to a file called chi_ADI.dat. Inside writetofile I have
const char* file_name = "foo";
FILE* chi_out = fopen(file_name,"wr");
and then I write using fprintf. So the problem is I have to specify a file name, so that's not useful for making several files. Is there a way to read off the name of the variable, so that my file is saved appropriately. Thanks for any help!
Symbol (and thus variable) names are merely a linguistic convenience, not an actual part of a program.
Fortunately, what you want to do can be done by simply passing a string to your function.
Honestly, I don't know how you didn't think of this first, rather than something as silly as passing information using a variable name. You were like one step away from this solution; you just needed to connect the facts that you're passing a string to fopen() and that you can store filenames in strings.
I have thought of this solution, but was not, and still am not very happy with it. This way I have to create extra variables, and then write everything twice. So I would have to write
writetofile(chi_SOR,chi_SOR,...)
or
writetofile(chi_ADI,chi_ADI,...)
where in the first instance it's a string and in the second an array. To me this is silly. Having to write it just once, and having the program do the work of reading off the name is actually logical and much more elegant. But what is even more silly is someone telling someone else that his problem is silly, without even providing the answer.
It is not, nor will it ever be, possible to get the name of the variable, unless it's name is specifically stored in another variable. The reason for this is that C++ is a compiled language, which means that none of the text in the source is saved when it's converted to an exe (excluding string variables).
@PiMaster
Actually you could, with the preprocessor. I frequently use this macro for debugging: #define PRINT(x) ::std::cout << #x << " = " << x << ::std::endl;
This way I have to create extra variables, and then write everything twice.
Not really. You can pass the string literal directly without storing it in an intermediate variable.
To me this is silly. Having to write it just once, and having the program do the work of reading off the name is actually logical and much more elegant.
Maybe, but since a program can't work on data that doesn't exist, such as a variable name, that's just not possible.
But what is even more silly is someone telling someone else that his problem is silly, without even providing the answer.
I said the idea of passing a filename through a variable was silly compared to the relatively simple task of just using a string, not that the problem was silly.
And you don't have to be so touchy. I didn't mean any offense. And I did give you an answer, which was:
1. that cannot be done, and
2. the same effect can be achieved by simply using a string.
R0mai: That's just an instance of storing the variable name in a string.