Hello, so firstly let me present my objective.
I want to create a function with a return type. However, I want to use it to read from a file (ifstream) and produce multiple different types of return types. The different file types returned would be always in the same order. For example
Text File:
Name
1
12
30
Area
I want to ifstream line 1 (Name) to an array of characters.
line 2, 3, and 4 to integers.
and line 5 as a string.
To my knowledge of c++ and googling and browsing web forum posts I have not found anything of the sort. I am not actually sure if this would be the best way to input different variables from a file, but I am not aware of any other ways than reading from the file.
The basic problem is that if I make a function with one return type, it would only return one type of data to my int main(). I suppose I could create multiple functions that would run this for different variable types and destroy the invalid types. But this seems inefficient.
Any ideas would be appreciated. It is being used to load in data from a previously saved file.
So far..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//function to load a player's data
string loaded(string fileName)
{
ifstream loadfile;
loadfile.open(fileName);
if (loadfile.fail())
{
cerr << "Error loading file.\n";
return 0;
}
// The following part is what I'm working on. So that when I call this
// function it will load the information and assign it to the
// appropriate variables.
while (!loadfile.eof())
{
}
}
|
I was thinking I could possibly do something with a counter to count the lines and assign a value based on their order. The problem is with the 1 return value of a function. Maybe there is another operation I could use?
Thank in advance.