-2

I'm new to c++ and i'm trying to read in a file and store it in a 9x9 array, can anyone explain how i can do this whilst keeping it as simple as possible? The aim is too make a Sudoku solver.

File looks like;

0 3 0 0 0 1 0 7 0

6 0 0 8 0 0 0 0 2

0 0 1 0 4 0 5 0 0

0 7 0 0 0 2 0 4 0

2 0 0 0 9 0 0 0 6

0 4 0 3 0 0 0 1 0

0 0 5 0 3 0 4 0 0

1 0 0 0 0 6 0 0 5

0 2 0 1 0 0 0 3 0

Thanks in advance.

  • 5
    You're probably going to be better off with a 3x3 array of 3x3 arrays IMHO. In answer to your question, this is not a site where we do things for you, however I will point you in the right direction. Take a look at [std::fstream](http://www.cplusplus.com/reference/fstream/fstream/) and [for](http://www.cplusplus.com/doc/tutorial/control/) loops. – OMGtechy Mar 26 '14 at 13:13
  • This answer is close enough: http://stackoverflow.com/a/12722940/104774 – stefaanv Mar 26 '14 at 13:24

2 Answers2

1

You need to use std::istringstream

For example

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

//...

const size_t N = 9;

int a[N][N] = {};

std::ifstream file( "SomeFileName" );

std::string line;

for ( size_t i = 0; i < N && std::getline( file, line ); i++ )
{
   std::istringstream is( line );

   size_t j = 0;

   while ( j < N && is >> a[i][j] ) j++;
}

If you should not bother that a line can contain more or less than 9 numbers per line then you can write simply

#include <iostream>
#include <fstream>

//...

const size_t N = 9;

int a[N][N] = {};

std::ifstream file( "SomeFileName" );

size_t i = 0;

while ( i < N * N && file >> a[i / N ][i % N] ) i++;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

There are several ways you could do this, all of which start with opening the file first though. To do this, you will need to include fstream and create a "file in" reader (look at the ifstream class).

Once you have that it's pretty straight forward. You can either grab character by character by just looping to the end of the file and storing the number accordingly OR you can grab the file line by line and then loop through each line to grab the numbers. If you do the later, there are methods that will help split the string into an array based on the spaces but I'll let you look that up. Also, getting the line will mean you will need to catch the newline character into a junk variable so it can continue on, else the next time you get a line it will just get that variable anyways.

for and while loops are going to be your friend in this.

Also, don't forget to close your file stream when you're done too.

EDIT: I forgot to mention that if you do the line by line way, you will need to use some method to turn a string/array into an integer (atoi is one, i also think there is a stoi but you may need to include string to use that or I'm mistaken about that one). Doing it character by character you can go straight into an integer, but you better make sure there isn't any non-number character in your file and that there won't be.

Josh Braun
  • 490
  • 2
  • 16
  • In line by line, you can put your line-string in an istringstream and read integers with operator<< (as in the answer of Vlad), so that's not much different from reading each number directly from file. – stefaanv Mar 26 '14 at 15:19