[英]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.