ifstream input to different file types

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct player_info
{
    std::string name ;
    int n1 ;
    int n2 ;
    int n3 ;
    std::string area ;
};

player_info load( std::string file_name )
{
    std::ifstream file(file_name) ;
    
    player_info info ;

    if( std::getline(file,info.name) && file >> info.n1 >> info.n2 >> info.n3 && 
        file.ignore(1000,'\n') && std::getline(file,info.area) ) return info ;
    
    else return info = {} ; // error, nothing was read
}
That worked fairly well. I did run into some weird business where some string names wouldn't be taken with std::getline(file,info.name) but if I created a separate string variable for name, then did std::getline(file,name) and then name=info.name; then that would work. Some of my strings worked with info.name and some did not. It was quite weird. This is the finished business that is working correctly. If anyone could point out why some of them are having to be passed by through separate type I would appreciate it. Thanks again for the help JLBorges.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
playerInfoStore load(string fileName)
{
	std::ifstream file(fileName);

	playerInfoStore info;

	string gender;
	string playerClass;
	string location;

	if (std::getline(file, info.name) && std::getline(file, info.gender) && std::getline(file, playerClass) && std::getline(file, location) >> info.charLevel >> info.attackLevel >> info.strengthLevel >> info.enduranceLevel >> info.armor >> info.money &&
		file.ignore(1000, '\n'))
	info.gender = gender;
	info.playerClass = playerClass;
	info.location = location;
	return info;
}


there are odd things. This code returns all the data types correctly, but if I take out <String gender> and info.gender = gender then the playerClass will not return, which the gender still returns properly. I'm not at all sure why this is. Thanks a million.
The statement governed by your if is line 13. If you remove line 13, then the statement governed by your if is what is now line 14.

In JLBorges' code, the statement governed by the if was return info;

Do you see what is happening now?
Oh. I don't really know how I overlooked that haha. Thanks cire, that helped me a lot!
Topic archived. No new replies allowed.