简体   繁体   中英

In C++, how do I go about turning different lines of a txt file into multiple variables?

I'm basically, trying to create a simple text adventure and each line in the save file will represent a certain string or integer.

The text file looks something like this:

Tyler
Boy
1a
0

The code looks like this:

 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") { is where I'm getting an error... the error is this: error: expected primary-expression before string constant

And I want each line to represent an individual variable.

So Tyler would be assigned to a string called name.

Boy would be assigned to a string called gender, etc. All I have so far is code that writes or in my case "saves" the data into a text file that won't be modified until the player saves the game again.

Looks like you could benefit from a container of structures.

Let's assume:

struct Player
{
  std::string name;
  std::string gender;
  // other data
};

You could implement a std::vector<Player> to hold instances (or in your terms variables ) of Players. This is the usual technique among programmers.

As Thomas Matthews says, you're better off wrapping all your variables up in a nice struct bundle:

struct Player 
{
    std::string Name;
    std::string Gender;
    std::string Data;
    int OtherData;
};

As for 'loading' from a file, you could easily create a constructor to load the data into the Player object:

#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...
    }
}

I can get you some directions but they are far away from completed. You can work more on error handling, output formatting, class design 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;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM