C++ 如何从文本文件填充指针数组?

C++ 如何从文本文件填充指针数组?,c++,text-files,C++,Text Files,我正在做一个学校的项目,今天就要交了,我被一个可能很简单的问题困住了 我需要做一个游戏刽子手,而我一直坚持的任务是从文本文件中填充一系列指针,我需要阅读错误答案的图片 void ReadScenes(string *scenes[10]) { ifstream inFile("Scenes.txt"); if (inFile.is_open()) { int i = 0; string scene; while ((inFile >> scene)&

我正在做一个学校的项目,今天就要交了,我被一个可能很简单的问题困住了

我需要做一个游戏刽子手,而我一直坚持的任务是从文本文件中填充一系列指针,我需要阅读错误答案的图片

void ReadScenes(string *scenes[10])
 {
ifstream inFile("Scenes.txt");
if (inFile.is_open())
{
    int i = 0;
    string scene;
    while ((inFile >> scene)&&(i<10))
    {
        *scenes[i] = scene;
            i++;        
    }
}
}
int main()
{
  char *scenes[10];
  ReadScenes(scenes);
}
等等


方法中的代码用于读取密码,因为它们用空格分隔。所以我有10个场景,我想把它们保存在数组中

一个问题是,您认为您读取的文件应该是具有变量声明的类似C++的文件。这不是它的工作原理

你应该把文件的内容放进一个普通的C++源文件中并用它来构建。 差不多

std::string scenes[] = {
    // Scene 1
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "       *              \n"
    "      * *             \n"
    "     *   *            \n",

    // Scene 2
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "      * *             \n"
    "     *   *            \n",

    // And so on...
};
如果使用IDE,请将源文件添加到项目中

如果使用例如g++在命令行上构建,则

g++ -Wall main.cpp scenes.cpp -o my_program
其中scenes.cpp是包含场景数组的源文件

如果你需要使用外部文本文件,而不需要任何C++代码,那么它实际上是非常简单的:只需存储文本,就像没有引用或类似C++声明或语句的任何东西一样。 因为您知道每个场景正好有九行,可能还有一行额外的行来分隔两个场景,所以您可以使用for循环来读取该行

所以你的文本文件看起来像

* * * * * * * * * * * * * * * * 然后加载它

constexpr size_t NUMBER_OF_SCENES = 2;  // With the example scene file in this answer
constexpr size_t LINES_PER_SCENE = 9;

std::ifstream scene_file("scenes.txt");

std::array<std::string, NUMBER_OF_SCENES> scenes;

// Loop to read all scenes
for (std::string& scene : scenes)
{
    std::string line;

    // Loop to read all lines for a single scene
    for (unsigned line_number = 0; line_number < LINES_PER_SCENE && std::getline(scene_file, line); ++line_number)
    {
        // Add line to the current scene
        scene += line + '\n';
    }

    // Read the empty line between scenes
    // Don't care about errors here
    std::getline(scene_file, line);
}

一个问题是,您认为您读取的文件应该是具有变量声明的类似C++的文件。这不是它的工作原理

你应该把文件的内容放进一个普通的C++源文件中并用它来构建。 差不多

std::string scenes[] = {
    // Scene 1
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "       *              \n"
    "      * *             \n"
    "     *   *            \n",

    // Scene 2
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "      * *             \n"
    "     *   *            \n",

    // And so on...
};
如果使用IDE,请将源文件添加到项目中

如果使用例如g++在命令行上构建,则

g++ -Wall main.cpp scenes.cpp -o my_program
其中scenes.cpp是包含场景数组的源文件

如果你需要使用外部文本文件,而不需要任何C++代码,那么它实际上是非常简单的:只需存储文本,就像没有引用或类似C++声明或语句的任何东西一样。 因为您知道每个场景正好有九行,可能还有一行额外的行来分隔两个场景,所以您可以使用for循环来读取该行

所以你的文本文件看起来像

* * * * * * * * * * * * * * * * 然后加载它

constexpr size_t NUMBER_OF_SCENES = 2;  // With the example scene file in this answer
constexpr size_t LINES_PER_SCENE = 9;

std::ifstream scene_file("scenes.txt");

std::array<std::string, NUMBER_OF_SCENES> scenes;

// Loop to read all scenes
for (std::string& scene : scenes)
{
    std::string line;

    // Loop to read all lines for a single scene
    for (unsigned line_number = 0; line_number < LINES_PER_SCENE && std::getline(scene_file, line); ++line_number)
    {
        // Add line to the current scene
        scene += line + '\n';
    }

    // Read the empty line between scenes
    // Don't care about errors here
    std::getline(scene_file, line);
}

如果你陷入困境,主要是因为你把解决方案复杂化了。不需要使用指针数组

相反,只需将屏幕存储为字符串数组,并使用std::getline从文件中读取它们:

std::array<std::string, 3> read_file(std::istream& s) {
    std::array<std::string, 3> result;
    for (auto & row : result)
        std::getline(s, row);
    return result;
}
您还调用了变量定义文件;不一样。文件通常驻留在磁盘上。由于std::istream接口可以接受基于内存的std::stringstream(如我的示例所示)或std::ifstreamyour_path.txt,上述解决方案足够灵活

如果您最初打算将所有内容存储在源代码中,那么简单地将所有内容存储在需要使用的形式听起来是一个更好的选择


.

如果您陷入困境,主要是因为您使解决方案过于复杂。不需要使用指针数组

相反,只需将屏幕存储为字符串数组,并使用std::getline从文件中读取它们:

std::array<std::string, 3> read_file(std::istream& s) {
    std::array<std::string, 3> result;
    for (auto & row : result)
        std::getline(s, row);
    return result;
}
您还调用了变量定义文件;不一样。文件通常驻留在磁盘上。由于std::istream接口可以接受基于内存的std::stringstream(如我的示例所示)或std::ifstreamyour_path.txt,上述解决方案足够灵活

如果您最初打算将所有内容存储在源代码中,那么简单地将所有内容存储在需要使用的形式听起来是一个更好的选择


.

我一直坚持的是填充一个指针数组-然后不要使用指针数组?ReadScenes接受一个std::string**但你给它char**。不起作用。为什么它是一个指针数组?可以用指针填充数组,但如果这些指针不指向任何地方,那就毫无意义了。您还需要将字符串本身存储在某个位置。通常,如果需要在容器中存储一些实例,并且使用指针容器,那么仍然需要将实例存储在某个位置。只需使用一个对象数组而不是指针数组就可以了今天到期的SOMG>\u我坚持的是填充一个指针数组-然后不要使用指针数组?ReadScenes需要一个std::string**但你给它char**。不起作用。为什么它是一个指针数组?可以用指针填充数组,但如果这些指针不指向任何地方,那就毫无意义了。您还需要将字符串本身存储在某个位置。通常,如果需要在容器中存储一些实例,并且使用指针容器,那么仍然需要将实例存储在某个位置。只需使用一个对象数组而不是指针数组就可以了,今天到期了>\u我可能不用指针就可以了,但我仍然不知道怎么做。但是我不能将场景保存在.cpp文件中,它们必须是.txt文件,因为老师要求我们这样做。@Jure那么你需要重新考虑文件的格式。不能使用C++声明
除非您想开发一个C++声明分析器,否则C++中的字符串文字可能会有点超出您的任务范围。用示例文件和代码更新了答案我可能在没有指针的情况下做,但我仍然不知道如何做。但是我不能将场景保存在.cpp文件中,它们必须是.txt文件,因为老师要求我们这样做。@Jure那么你需要重新考虑文件的格式。除非您想开发一个C++声明分析器,否则不能在其中使用C++声明或C++字符串文字,这可能超出了您的任务范围。用示例文件和代码更新了答案