C++ 在C++;,如何将txt文件的不同行转换为多个变量?
我基本上是在尝试创建一个简单的文本冒险,保存文件中的每一行都将代表一个特定的字符串或整数 文本文件如下所示:C++ 在C++;,如何将txt文件的不同行转换为多个变量?,c++,file,text,C++,File,Text,我基本上是在尝试创建一个简单的文本冒险,保存文件中的每一行都将代表一个特定的字符串或整数 文本文件如下所示: 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") {
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;
}
}
}
代码如下所示:
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;
}
}
}
void open_file1(){
结构文件1{
std::字符串名;
std::字符串性别;
std::字符串位置;
国际黄金;
file1(std::string);
};
file1::file1(std::string“file1.txt”){
std::ifstream输入(“file1.txt”.c_str());
if(Input.is_open()){
std::getline(输入,this->Name);//将第一行读入“Name”
std::getline(输入,this->Gender);//将第二行读入'Gender'
std::getline(输入,this->Location);//将第三行读入“Location”
std:getline(输入,this->Gold);//将第四行读作'Gold'
cout看起来您可以从结构容器中获益
让我们假设:
struct Player
{
std::string name;
std::string gender;
// other data
};
您可以实现一个std::vector
来保存播放器的实例(或者用您的术语变量)。这是程序员的常用技术。正如Thomas Matthews所说,您最好将所有变量包装在一个漂亮的结构
包中:
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...
}
}
#包含//以便我们可以使用std::ifstream
结构播放器
{
std::字符串名;
std::字符串性别;
std::字符串数据;
int其他数据;
Player(std::string);//我们的自定义构造函数
};
播放器::播放器(std::字符串文件名){
std::ifstream输入(fileName.c_str());
if(Input.is_open()){
std::getline(输入,this->Name);//将第一行读入“Name”
std::getline(输入,this->Gender);//将第二行读入'Gender'
//等等。。。
}
}
我可以帮你找到一些方向,但还远没有完成。
您可以在错误处理、输出格式、类设计等方面进行更多工作
#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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用std::string;
//Player.h
职业选手
{
公众:
//建设者
播放器(字符串名称、字符串生成器、字符串插入位置、大小\u t inGold)
:mName{inName},mGender{inGender}
,mLocation{inLocation},mGold{inGold}{
//默认构造函数
Player()=默认值;
//复制构造函数
玩家(康斯特玩家和玩家)
:mName{player.mName},mGender{player.mGender}
,mLocation{player.mLocation},mGold{player.mGold}{
//此构造函数仅用于演示,请将实现移动到Player.cpp
播放器(常量字符串和文件名)
{
//打开文件并检查错误。
ifstream infle{filename.c_str()};
如果(!infle){
抛出无效的_参数(“无法打开文件”);}
//一次读一个名字。
用于(播放器;内嵌>>播放器;){
//您可以检查该播放器是否有效
mName=player.mName;
mGender=player.mGender;
mLocation=player.mLocation;
mGold=player.mGold;
}
infle.close();
}
私人:
字符串mName;
串式加料机;
串定位;
尺寸(单位:m);
friend std::ostream&operator(std::istream&in,Player&Player);
};
内联标准::istream&operator>>(标准::istream&in,播放器&播放器)
{
返回>>player.mName>>player.mGender
>>player.mLocation>>player.mGold;
}
内联std::ostream&operator建议使用std::vector
或数组,而不是尝试创建变量名。变量名仅在编译时存在,并且不是可执行文件的一部分(在调试可执行文件中,它们可能是符号字典的一部分)。试着添加更多关于你想做什么的内容,并添加一些你已经开始编写的代码?你应该解释到底是什么阻止了你这么做。我不知道怎么做-_-