简体   繁体   English

C ++读取包含多种变量的txt文件

[英]C++ Reading a txt file containing multiple kinds of variables

My skills are very basic. 我的技能很基础。 I'm trying to make save and load functions for a text game. 我正在尝试为文本游戏创建保存和加载功能。 This is my code: 这是我的代码:

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>

#include "variables.h"

// CORE FUNCTIONS
void save_game()
{
    std::ofstream file((savefile_path + "/" + player_name + ".txt").c_str());
    if (file.is_open())
    {
        file << engine_switch << std::endl;  //int
        file << map_switch << std::endl;     // int
        file << sub_map_switch << std::endl;  // int
        file << player_name << std::endl;  //string
        file << player_class << std::endl;  // string
        //file <<  << endl;
        file.close();
    }
    else
    {
        std::cout << "A PROBLEM OCCURED";
        system("pause");
    }
    return;
}

void load_game()
{
    system("cls");

    std::cout << "ENTER THE SAVE FILE NAME (SAME AS YOUR CHARACTER NAME)\nOR PRESS ENTER TO GO BACK TO MAIN MENU: ";
    fflush(stdin);
    getline(std::cin, player_name);
    std::string name=player_name;
    std::ifstream file((savefile_path + "/" + player_name + ".txt").c_str());
    if(file)
    {
        file >> engine_switch; // this is int
        file >> map_switch; // this is int
        file >> sub_map_switch;  /this is int
        file >> player_name;  //string
        file >> player_class; //string
        //file >> ;
        return;
    }
    else
    {
        if(player_name=="\0")
        {
            engine_switch=1;
            return;
        }
        else
        {
            system("cls");

            std::cout << "COULDN'T OPEN THE SAVE FILE" << std::endl;
            system("pause");
            load_game();
        }
    }
    engine_switch=1;
    return;
}

The problem happens when I enter a player_name compound of multiple words separated with a space. 当我输入一个用空格分隔的多个单词的player_name复合词时,就会发生问题。 For example when I enter "name name" the player_name becomes name and the player_class becomes name and the actual player_class is not put into any variable. 例如,当我输入“名称名称”时, player_name变为name,而player_class变为name,并且实际的player_class不会放入任何变量中。

I tried the rdbuf() function, but didn't work and I don't even understand it yet. 我尝试了rdbuf()函数,但是没有用,我什至还不了解。 I tried it with stream , string , c.str() , everything I found on the web and could comprehend, but it always comes out wrong. 我用streamstringc.str()尝试了它,我在网上找到的所有内容都可以理解,但是总是出错。

You need to use getline instead of the >> operator in your load-game function. 您需要在load-game函数中使用getline而不是>>运算符。

Instead of doing file >> player_name do getline(file, player_name) This should be used in replacement of every occurrence of file >> someVar 而不是执行file >> player_name执行getline(file, player_name)来替换每次出现的file >> someVar

EDIT : I didn't realize the others were integer values. 编辑 :我不知道其他人是整数值。 For those you should still be using the >> operator. 对于那些,您仍应使用>>运算符。

When you extract a string from a stream, spaces are considered as separators. 从流中提取字符串时,空格被视为分隔符。
It's even worse: in your code, the player_name is followed by player_class which is also a string. 更糟糕的是:在您的代码中, player_name之后是player_class ,后者也是一个字符串。 How should your program interpret this : 您的程序应如何解释:

10 15 20 John William Doe hero B 

How would your program guess that John William Doe is a composed name and hero B the category ? 您的程序将如何猜测John William Doe是一个有名字的名字,而hero B这个类别?

The simplest solution is to write all your strings on a separate line in the file. 最简单的解决方案是将所有字符串写在文件中的单独一行上。 When you load it you can then read it with getline() : 加载时,可以使用getline()读取它:

file >> engine_switch; // this is int
file >> map_switch; // this is int
file >> sub_map_switch;  /this is int
getline (file, player_name);  //string
getline (file, player_class; //string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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