J
jccorreu
I've got to read info from multiple files that will be given to me. I
know the format and what the data is. The thing is each time we run
the program we may be using a differnt number of files, with different
file names each time. So i'm writing into the code to ask the user how
many files, and what their names are. From each we'll read in 2 lines,
then do some math using all of those lines. Then do it again on another
set of lines. I'm having some trouble creating different objects with
different names when I don't know before hand how many there will be or
what the file names will be. I know the following code won't work but
it might give an idea of what I'm thinking.
int num;
std::cout << "enter number of files: ";
std::cin >> num;
char* infile[num+1];
for(int n=1; n<=num; n++)
{
std::cout << "\nenter name of file " << n << " : ";
std::cin >> infile[n];
std::ifstream infile[n];
infile[n].open(infile[n]);
}
Anyone got any idea how to create variables whose names are themselves
variable by the program? It's something an old macro language I used
to know could do, but I don't yet see a way to manipulate c++ into it.
how could I force the creation of the ifstream object to take its name
from such a variable, or from an element of an array?
should I be using pointers in a different way?
would I have to somehow overload the fstream:pen() function?
any other methods are also welcome, doesn't have to be fstream if there
is something else. though this is some kind of process that I'd like
to make more general and applicable for other uses.
thanks all
James
know the format and what the data is. The thing is each time we run
the program we may be using a differnt number of files, with different
file names each time. So i'm writing into the code to ask the user how
many files, and what their names are. From each we'll read in 2 lines,
then do some math using all of those lines. Then do it again on another
set of lines. I'm having some trouble creating different objects with
different names when I don't know before hand how many there will be or
what the file names will be. I know the following code won't work but
it might give an idea of what I'm thinking.
int num;
std::cout << "enter number of files: ";
std::cin >> num;
char* infile[num+1];
for(int n=1; n<=num; n++)
{
std::cout << "\nenter name of file " << n << " : ";
std::cin >> infile[n];
std::ifstream infile[n];
infile[n].open(infile[n]);
}
Anyone got any idea how to create variables whose names are themselves
variable by the program? It's something an old macro language I used
to know could do, but I don't yet see a way to manipulate c++ into it.
how could I force the creation of the ifstream object to take its name
from such a variable, or from an element of an array?
should I be using pointers in a different way?
would I have to somehow overload the fstream:pen() function?
any other methods are also welcome, doesn't have to be fstream if there
is something else. though this is some kind of process that I'd like
to make more general and applicable for other uses.
thanks all
James