[英]In C++, how do I go about turning different lines of a txt file into multiple variables?
我基本上是在嘗試創建簡單的文本冒險,保存文件中的每一行都將代表某個字符串或整數。
文本文件如下所示:
Tyler
Boy
1a
0
代碼如下:
void open_file1() {
struct file1 {
std::string Name;
std::string Gender;
std::string Location;
int Gold;
file1(std::string);
};
file1::file1(std::string"file1.txt") {
std::ifstream Input("file1.txt".c_str());
if (Input.is_open()) {
std::getline(Input, this->Name); //Reads first line into 'Name'
std::getline(Input, this->Gender); //Reads second line into 'Gender'
std::getline(Input, this->Location); //Reads third line into 'Location'
std:getline(Input, this->Gold); //Reads 4th line as 'Gold'
cout << file1;
}
}
}
file1 :: file1(std :: string“ file1.txt”){是我收到錯誤的位置...錯誤是這樣的:error:字符串常量之前的預期主表達式
我希望每一行代表一個單獨的變量。
因此, 泰勒將被分配給一個名為name的字符串。
男孩將被分配給一個名為性別的字符串,依此類推。到目前為止,我所能做的就是編寫代碼,或者在我的情況下將數據“保存”到一個文本文件中,直到玩家再次保存游戲后才可以對其進行修改。
看起來您可以從結構容器中受益。
假設:
struct Player
{
std::string name;
std::string gender;
// other data
};
您可以實現std::vector<Player>
來保存std::vector<Player>
實例(或用您的術語說, 變量 )。 這是程序員常用的技術。
正如托馬斯·馬修斯(Thomas Matthews)所說,最好將所有變量包裝在一個好的struct
包中:
struct Player
{
std::string Name;
std::string Gender;
std::string Data;
int OtherData;
};
至於從文件“加載”,您可以輕松創建一個構造函數以將數據加載到Player
對象中:
#include <fstream> //So we can use std::ifstream
struct Player
{
std::string Name;
std::string Gender;
std::string Data;
int OtherData;
Player(std::string); //Our custom constructor
};
Player::Player(std::string fileName) {
std::ifstream Input(fileName.c_str());
if (Input.is_open()) {
std::getline(Input, this->Name); //Reads first line into 'Name'
std::getline(Input, this->Gender); //Reads second line into 'Gender'
//etc...
}
}
我可以為您提供一些指導,但距離完成還很遙遠。 您可以在錯誤處理,輸出格式設置,類設計等方面進行更多工作。
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using std::string;
// Player.h
class Player
{
public:
// Constructors
Player(string inName, string inGender, string inLocation, size_t inGold)
: mName {inName}, mGender {inGender}
, mLocation {inLocation}, mGold {inGold} {}
// Default constructor
Player()=default;
// Copy constructor
Player(const Player& player)
: mName {player.mName}, mGender {player.mGender}
, mLocation {player.mLocation}, mGold {player.mGold} {}
// This constructor is for demo only, move implementation to Player.cpp
Player(const string& filename)
{
// Open the file and check for errors.
ifstream inFile {filename.c_str()};
if (!inFile) {
throw invalid_argument("Unable to open file"); }
// Read the names one at a time.
for (Player player; inFile >> player; ) {
// You can check that player is valid
mName = player.mName;
mGender = player.mGender;
mLocation = player.mLocation;
mGold = player.mGold;
}
inFile.close();
}
private:
string mName;
string mGender;
string mLocation;
size_t mGold;
friend std::ostream& operator<<(std::ostream& stream, const Player& player);
friend std::istream& operator>>(std::istream& in, Player& player);
};
inline std::istream& operator>>(std::istream& in, Player& player)
{
return in >> player.mName >> player.mGender
>> player.mLocation >> player.mGold;
}
inline std::ostream& operator<<(std::ostream& out, const Player& player)
{
return out << std::setw(10) << player.mName << ' '
<< std::setw(10) << player.mGender << ' '
<< std::setw(10) << player.mLocation << ' '
<< std::setw(10) << player.mGold << '\n';
}
// Test app
int main()
{
Player player1 {"player1.txt"};
cout << "Player 1: " << player1 << endl;
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.