Jure
Jure

Reputation: 13

How to fill pointer array from text file?

I'm working on a school project that is due today, and I'm stuck on a probably simple problem.

I need to make the game "Hangman" and the task I'm stuck on is to fill an array of pointers from text file (I need to read the pictures for wrong answers).

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);
}

And my text file looks like this:

char *scene1 =
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "                      \n"
    "       *              \n"
    "      * *             \n"
    "     *   *            \n";


char *scene2 =
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "       *              \n"
    "      * *             \n"
    "     *   *            \n";

And so on.

The code in method was working for reading passwords because they were separated with spaces. So I have 10 scenes and I want to save them in array.

Upvotes: 1

Views: 1099

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409146

One problem is that you think that the files you read should be C++-like files with variable declarations. That's not how it works.

You should put the contents of the file into a normal C++ source file and build with it.

Something like

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

If you use an IDE add the source file to your project.

If you use e.g. g++ to build on the command-line then

g++ -Wall main.cpp scenes.cpp -o my_program

Where scenes.cpp is the source file containing the scenes array.


If you need to use external text files, without any C++ code, then it's actually very easy: Just store the text, as is, without quotes or anything resembling C++ declarations or statements.

Since you know that each "scene" is exactly nine lines (and perhaps an extra line to separate two scenes) you use a for loop to read the line.

So your text file could then look like







           *              
          * *             
         *   *            

           *              
           *              
           *              
           *              
           *              
           *              
           *              
          * *             
         *   *            

Then to load it

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);
}

Upvotes: 6

Bartek Banachewicz
Bartek Banachewicz

Reputation: 39370

If you got stuck, it's mostly because you overcomplicated the solution. There's no need to use an array of pointers.

Instead, simply store your screen as an array of strings, and use std::getline to read them from file:

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;
}

You've also called your variable definitions "files"; it's not the same. Typically files reside on a disk. The solution above is flexible enough (thanks to std::istream interface) to accept either a memory-based std::stringstream, like in my example, or e.g. std::ifstream("your_path.txt").

If you originally intended to store everything in source code, simply storing everything in the form it needs to be used sounds like a better option.

Working example.

Upvotes: 1

Related Questions