0

So I'm having a little bit of trouble reading from a text file in C++. What I'm trying to do is read from a text file that contains a grid of letters. Here's what I have set up:

int main()
{
    char grid[10][10];
    ifstream input;
    input.open("puzzle1_size10.txt");

    //what goes here

    input.close();
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 10; j++)
            cout<< grid[i][j]<<" ";
        cout<<endl;
    }
}

The text file is this:

10 10
f e h l o a k r a y 
e s r r t s c d o y 
a l u g d o e e g a 
t c y y m h l j y a 
u r a p s y n a r j 
r e u d c a e p e r 
e t s o c h t p h c 
e g o p e h w l t w 
h h c l s d o e c a 
l n h c a m r l e e 

The first two numbers represent the size of the grid. I'm trying to read each character (ignoring the initial numbers) and store the character into the array called grid. Any advice?

There are other similar questions I've found on stackoverflow but none have the same context as this and none of them give me a definitive answer. I am simply trying to read one character at a time from a file and store that character into a 2D array so that each letter in the array can be easily referenced. Can someone please help?

N. Sunilkumar
  • 65
  • 1
  • 7
  • 1
    Try looking for how to read characters/lines in from a file in c++. The 10 10 makes easier because you know when the end of each line/file is. After you give it a try, edit your question to show what you have tried in that //what goes here area. – KPrince36 Oct 13 '15 at 17:23

2 Answers2

1

I believe you're looking for something like this?

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char grid[10][10];
    ifstream input;
    input.open("File.txt", ios::in);

    int column = 0;
    int row = 0;


    input >> column >> row;

    for (int column_counter = 0; column_counter < column; column_counter++){

        for (int row_counter = 0; row_counter < row; row_counter++){

            input >> grid[column_counter][row_counter];

        }
    }



    input.close();
    for(int i = 0; i < 10; i++)
    {
        for(int j = 0; j < 10; j++)
            cout<< grid[i][j]<<" " << endl;
        cout<<endl;
    }
}

I first took the first two numbers in and named them column and row. Then in two for loops I added counters to count the position of the grid until they reach what you specified in the beginning of the file.

If you have any questions just ask! I'll add comments to them soon. I'm currently in a cafe right now.

Santosderek
  • 106
  • 6
  • Thank you, this solves my problem! Coincidentally it also helped me realize what the problem was: I went to my documents folder and found that the file I was reading from wasn't labeled as a .txt file. Renamed it and now I have my grid (before renaming it the terminal outputted a bunch of random characters in no order). Thanks so much!! – N. Sunilkumar Oct 13 '15 at 17:57
  • @N.Sunilkumar No problem! I'm glad it helped! – Santosderek Oct 13 '15 at 17:59
0

Since this is tagged c++, I recommend you to use std::vector instead of normal array, this way you have a way more flexible approach

std::string line;
getline(file, line); // skip first line
std::vector<std::vector<char>> grid;
while(getline(file, line))
{
    std::vector<char> insideV (line.begin(), line.end()); // read string into vector of char
    grid.push_back(insideV);
}

......
std::cout << grid[0][0]; // f
hlscalon
  • 7,304
  • 4
  • 33
  • 40