i am searching the internet a while now but cant find a solution thats working for me. or i am just dumb. I have 12 lines in a.txt file and i want to read those line by line, one after another. starting with 1,2...12. my.txt file looks like this:
ITEM1 (string)
ITEM2 (string)
ITEM3 (string)
ITEM4 (string)
ITEM5 (string)
ITEM6 (string)
ITEM7 (string)
ITEM8 (string)
ITEM9 (string)
ITEM10 (string)
1 (INT)
NATHAN (string)
whats written in brackets is not written in the file. its just the way it was saved from the program. i wanna read the file and store the text in the variables. The ITEM1-ITEM10 are from an array called inventory[10]. The number '1' is just a marker and goes in the variable 'int gameposition'. The name 'NATHAN' goes in a String 'string charactername'.
File path is:
C:\apoadventure\savegame.txt
i know that this type of saving savegame information isnt very clever but im new to this and im just trying and learning.
My InputFile.txt
Apple 1
Mango 2
Banana 3
Grape 4
My main body
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream file;
file.open("InputFile.txt");
std::string str[4];
for (int i = 0; std::getline(file, str[i]);i++)
{
std::cout << str[i] << "\n";
}
file.close();
}
Now in the above code i am inputting the data from the.txt file line by line by using the getline() function.
This is my output
Apple 1
Mango 2
Banana 3
Grape 4
Hope this helped you out in how to input data from a file line by line:-)
You have a line based format, so read it with getline
. Anything that isn't a string can be converted from the string just read (eg stoi
in the code below)
string inventory[10], charactername, work;
int gameposition;
for (int i = 0; i < 10; ++i)
getline(file, inventory[i]);
getline(file, work);
gameposition = stoi(work);
getline(file, charactername);
UPDATE
cout << "Gameposition: " + gameposition << endl;
cout << "Your character: " + selectedChar << endl;
should be
cout << "Gameposition: " << gameposition << endl;
cout << "Your character: " << selectedChar << endl;
This part is working so far.
txt file contains:
ITEM1
ITEM2
ITEM3
ITEM4
ITEM5
ITEM6
ITEM7
ITEM8
ITEM9
ITEM10
0
NATHAN
string savedinv[10], charactername, work;
int gameposition;
ifstream file;
file.open("C:/apoadventure/savegame.txt");
cout << "Inventory loaded:\n";
cout << "\n";
for (int i = 0; i <= 9; ++i) {
getline(file, savedinv[i]);
cout << savedinv[i] << "\n";
}
cout << "\n";
getline(file, work);
gameposition = stoi(work);
getline(file, charactername);
selectedChar = charactername;
file.close();
//handling information
//inventory + gameposition + selectedChar
inventory[0] = savedinv[0];
inventory[1] = savedinv[1];
inventory[2] = savedinv[2];
inventory[3] = savedinv[3];
inventory[4] = savedinv[4];
inventory[5] = savedinv[5];
inventory[6] = savedinv[6];
inventory[7] = savedinv[7];
inventory[8] = savedinv[8];
inventory[9] = savedinv[9];
But when i try to call the gameposition it gives me NOTHING back.
cout << "Gameposition: " + gameposition << endl;
cout << "Your character: " + selectedChar << endl;
cout << "\n";
OUTPUT:
ITEM1
ITEM2
ITEM3
ITEM4
ITEM5
ITEM6
ITEM7
ITEM8
ITEM9
ITEM10
Gameposition:
Your character: NATHAN
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.