Reading in chars including blank spaces but not new line

I am trying to read in a text file of characters including blank spaces. I've tried a few solutions but all of different problems.

The input file looks like this:
1
2
3
4
5
6
0000000
       0
00 00 0
0  00000
0000000
0000000


(this text file input isn't formatting correctly, but I hope you get the idea- it's correct in my program)

1
2
3
4
5
6
7
8
9
10
11
12
  char** arr = new char*[rows];
  for (int i = 0; i < rows; i ++)
  {
       arr[i] = new char[columns];
       for (int j = 0; j < columns; j++)
       {
            std::getline(inFile,line);
            temp = line[j];
            arr[i][j] = temp;
       }
       std::cout<<"\n";
  }

This code displays the correct number of rows but every character appears as a 0.

1
2
3
4
5
6
7
8
9
10
11
  char** arr = new char*[rows];
  for (int i = 0; i < rows; i ++)
  {
       arr[i] = new char[columns];
       for (int j = 0; j < columns; j++)
       {
            inFile.get(temp);
            arr[i][j] = temp;
       }
       std::cout<<"\n";
  }

This is the closest I have this to working, it displays correctly but the newline char is included in the file so there appears to be an extra blank line between each row.

1
2
3
4
5
6
7
8
9
10
11
12
13
  char** arr = new char*[rows];
  for (int i = 0; i < rows; i ++)
  {
       arr[i] = new char[columns];
       for (int j = 0; j < columns; j++)
       {
            inFile.get(temp);
            std::cin.ignore(temp, '\n');
            arr[i][j] = temp;

       }
       std::cout<<"\n";
  }

This one crashed my program for some reason.

Any ideas for how to fix it?
Last edited on
std::getline(inFile,line);

getline() would be the correct approach but you'd have to use std::string as the data-type for the line variable
http://www.cplusplus.com/reference/string/string/getline/
Whenever I do getline it reads in the first 3 lines as one line. Is there a way to fix this? I've tried rewriting the file a few times, but still encounter the same problem.
I fixed the problem, the getline needed to be moved outside of the second for loop.
Topic archived. No new replies allowed.