Search code examples
c++arraysmultidimensional-arrayifstreamchars

Trying to convert a text file (ifstream) into a 2D array of chars in c++


I've been trying for about 2 days and i think I've come close but still no cigar! I have a text file that looks like this:

++++++
+H   +
+    +
+    +
+   X+
++++++

if i try to print it out normally, everything works:

void Level::LevelPrinter()  //This prints out fine
{
    inputMap.open("Level1.txt");

    string input;

    while (getline(inputMap, input))
    {
    cout << input << endl;
    }

}

But when i try to place all these symbols into a 2D array I either get empty spaces in some forms of code that i tried; or, on my latest try that seems to be closest to what i need, i get weird symbols placed in correct positions. I can't figure out what's wrong...

This is the code i'm trying out now:

void Level::LevelPrinterArray()
{
    inputMap.open("Level1.txt");

    char MapSymbols;
    int Rows, Cols;

    {
        for (Rows = 0; Rows < 6; Rows++)
        {
            for (Cols = 0; Cols < 6; Cols++)
            {

                inputMap >> MapSymbols;

                MapLayoutArray[Rows][Cols] = MapSymbols;

                cout << MapSymbols;
            }
            cout << endl;
        }
    }


}

And this is what the console shows me:

╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠

btw, MapLayoutArray is just:

char MapLayoutArray[6][6];

RadLexus, i tried not using >> so it looks like this instead:

void Level::LevelPrinterArray()
{
    inputMap.open("Level1.txt");

    char MapSymbols;
    int Rows, Cols;

    {
        for (Rows = 0; Rows < 6; Rows++)
        {
            for (Cols = 0; Cols < 6; Cols++)
            {
                inputMap.get(MapSymbols);

                MapLayoutArray[Rows][Cols] = MapSymbols;

                cout << MapSymbols;
            }
            cout << endl;
        }
    }


}

That also prints out:

╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠
╠╠╠╠╠╠

-----UPDATE------

using .close on the first code (the one i said works properly). Made the characters on the 2nd code appear as normal characters! the problem is - they now looks like this:

++++++

+H
+
+
 +
+
  +
+
  X+
+

Played around a bit with the code and now this code:

void Level::LevelPrinterArray()
{
    inputMap.open("Level1.txt");

    char MapSymbols;

    {
        for (int Rows = 0; Rows < 6; Rows++)
        {
            for (int Cols = 0; Cols < 6; Cols++)
            {

                inputMap.get(MapSymbols);

                    if (MapSymbols != '\n')
                {
                    MapLayoutArray[Rows][Cols] = MapSymbols;

                    cout << MapSymbols;
                }

                    else
                    {
                        cout << endl;
                    }   
            }
        }
    }

    inputMap.close();
    cout << endl;
}

causes this:

++++++
+H   +
+    +
+    +
+   X+
+
Press any key to continue . . .

So i'm extremely close but i can't get it to print the last line. I tried many things to get it to print the last line like making the first for loop "Rows <= 6" but that prints out the last line properly and crashes the console with an error... I'll play around with this some more... If any of you has an idea, let me know. I'll update here if i figure it out...


Solution

  • I tried to solve your problem on my computer.

    The change I made is that I iterate the current line and insert it to the array char by char.

    this code works very well:

    #include <string>
    #include <iostream>
    #include <fstream>
    
    int main(){
    
        std::ifstream inputMap("C:\\Res\\Level1.txt");
    
        char arr[6][6];
        for (int i = 0; i < 6; i++)
        {
            std::string input;
            std::getline(inputMap, input);
            for (int k = 0; k < input.length(); k++)
            {
                arr[i][k] = input[k];
                std::cout << input[k];
            }
            std::cout << std::endl;
        }
    
        std::cout << std::endl;
    
        for (size_t l = 0; l < 6; l++)
        {
            for (size_t m = 0; m < 6; m++)
            {
                std::cout << arr[l][m];
            }
            std::cout << std::endl;
        }
    
        system("PAUSE");
        return 0;
    
    }
    

    The output is as desired (I copied it here).

    For your purpose, just don't print it in the first or second time ;)

    ++++++
    +H   +
    +    +
    +    +
    +   X+
    ++++++
    
    ++++++
    +H   +
    +    +
    +    +
    +   X+
    ++++++