user3504410
user3504410

Reputation: 183

How do I scan certain characters from a file

I am currently working on a sudoku validator, and the way I am doing it is to separate each row into an array of length 9 and check, separate each column into an array of length 9 and check, etc. I have done the rows already doing something like this

         for( i=0; i<9; i++)
        fscanf(file, "%1d", &row1[i]);
          for( i=0; i<9; i++)
        fscanf(file, "%1d", &row2[i]);
          for( i=0; i<9; i++)
        fscanf(file, "%1d", &row3[i]);
          for( i=0; i<9; i++)
        fscanf(file, "%1d", &row4[i]);

and it works the way I want it too.

I have each number in my sudoku solution separated by a space horizontally and next to each other vertically in my file.

However, I am having trouble trying to scan each column into an array. Basically what I think I need to do is for the first column start at the first (top-left) character and store that into the first slot of my array. Then I need to skip 9 characters and then store that character.

I thought I could do something similar like

         for( i=0; i<82; i += 9)
        fscanf(file, "%1d", &col1[i]);
          for( i=1; i<82; i+=9)
        fscanf(file, "%1d", &col2[i]);
          for( i=2; i<82; i+=9)
        fscanf(file, "%1d", &col3[i]);
          for( i=3; i<82; i+=9)
        fscanf(file, "%1d", &col4[i]);
          for( i=4; i<82; i+=9)

but I realized it didn't work because I had set my array to size 9 and since i was inserting into the ith value in the array this would not behave like it wanted.

Can anyone give me a suggestion how I could implement this? I think it is fairly trivial but I am having serious trouble wrapping my head around it.

Thanks

Edit: I also need to do this for the region, so i'm basically looking for a way to skip through a file and store only certain characters that I want.

Upvotes: 1

Views: 208

Answers (2)

Jongware
Jongware

Reputation: 22457

Evade all the counting and skipping and read as single characters, with a simple filter. After all, you are only interested in the 81 digits from 1 to 9, which will always appear in the correct order -- no matter what other formatting or neatification is in place. If you also include the 0 or . for unknown cells, you can read formats such as this

1..|...|7..
.2.|...|5..
6..|38.|...
-----------
.78|...|...
...|6.9|...
...|...|14.
-----------
...|.25|..9
..3|...|.6.
..4|...|..2

and also this

000605000003020800045090270500000001062000540400000007098060450006040700000203000

(examples picked randomly from http://www.sudocue.net/fileformats.php)

Upvotes: 0

a_pradhan
a_pradhan

Reputation: 3295

Since it is a sudoku validator, create a 9 * 9 2D array and read row wise much like you are doing. Then loop through either row wise or column wise to validate your sudoku square.

// As pointed out in comments, this code assumes you have a minimum of 9*9 separated ints with width = 1.
// If not, you need to check whether fscanf failed or not.
int sudoku[9][9] ;
for(int i = 0; i < 9; ++i)
    for(int j = 0; j < 9; ++j)
        fscanf(file_ptr, "%1d", &sudoku[i][j]); /* Check for the return value 
                                                 * of fscanf if it is not
                                                 * guaranteed that there 
                                                 * will always be 81 elements 
                                                 * to read from or some other 
                                                 * error occurred while 
                                                 * reading the file.
                                                 */

// now use sudoku to verify.
for(int i = 0; i < 9; ++i)
    for(int j = 0; j < 9; ++j)
    {
        // sudoku[i][j] is row wise.
        // sudoku[j][i] is column wise.
    }

I am assuming you are using C99 or up compiler. If it is C89, you need to move the int i and int j declarations at the beginning of the function (outside the loop).

Upvotes: 3

Related Questions