简体   繁体   English

Visual C ++从文件中保存/读取多个变量

[英]Visual C++ Save/Read Multiple Variables From A File

This is my first post and I'm fairly new to C++. 这是我的第一篇文章,对C ++还是相当陌生。 I am currently looking for a way to save multiple variables to a file (XML or TXT) so it looks like this: 我目前正在寻找一种将多个变量保存到文件(XML或TXT)的方法,如下所示:

charactername:George
level:5

I would also like to be able to read these and put them into a variable. 我也希望能够阅读这些内容并将它们放入变量中。

Ex: 例如:

std::string characterName = "George";

(but it would read George from the line in the file charactername:George) (但它将从文件字符名:George中的行中读取George)

I have a total of 68 variables (48 strings, 11 ints, and 9 bools) I want in 1 file. 我总共需要1个文件中的68个变量(48个字符串,11个整数和9个布尔值)。

Does anyone know a way to do this or a tutorial they could point me towards? 有谁知道这样做的方法或他们可以为我提供指导的教程? I have found was to save 1 string to a file, but not multiple variables of different types. 我发现是将1个字符串保存到文件中,但不是多个不同类型的变量。

I think you should learn how to use a datafile matrix, 我认为您应该学习如何使用数据文件矩阵,

But before that here is some basic file management code for you to try use, you'll be able to read in data and recover it based on a structured layout, when recovering your bool data use an implicit conversion to change from a string. 但是在此之前,这里有一些基本的文件管理代码供您尝试使用,您将能够读取数据并根据结构化布局对其进行恢复,而在恢复布尔数据时,请使用隐式转换来更改字符串。

Here are some basic file operations, this will create a txt file that has data on new lines: 以下是一些基本的文件操作,这将创建一个txt文件,其中包含新行中的数据:

    // basic file operations
    // writing on a text file
    #include <iostream>
    #include <fstream>
    using namespace std;

    int main () {
      ofstream myfile ("example.txt");
      if (myfile.is_open())
      {
        myfile << "This is a line.\n";
        myfile << "This is another line.\n";  // this will for data onto a new line to be read later.
        myfile.close();
      }
      else cout << "Unable to open file";
      return 0;
    }

How to recover data, this will put the data into a string array which you can then use to recall data from in your code: 如何恢复数据,这会将数据放入一个字符串数组,然后您可以使用它从代码中调用数据:

    // how to retrieve the data:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;

    int main () {

      string line, data_array[67];
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while ( getline (myfile,line) )
        {
           data_array[i] = line; i++;
        }
        myfile.close();
      }

      else cout << "Unable to open file"; 

      return 0;
    }

How to edit data, you'll need to have a function to read in all your variables and rewrite the whole text file as unless each line is exactly the same byte you can not jump directly to it. 如何编辑数据,您将需要有一个函数来读取所有变量并重写整个文本文件,除非每一行与您不能直接跳转到的字节完全相同。

To look more into detail you should learn how to use a datafile matrix, here are some nice videos to get you started.: 要更详细地了解您,您应该学习如何使用数据文件矩阵,以下是一些不错的视频,可以帮助您入门:

C++ Tutorial (Reading Rows and Columns from datafile Matrix C ++教程(从数据文件矩阵读取行和列

Matrix in C++ | C ++中的矩阵| Part #1 | 第一部分| simple matrix definition using arrays 使用数组的简单矩阵定义

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

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