View Single Post
Old 10-28-2005, 08:47 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
If the file is presented as:
Code:
0123456789
1234567890
2345678901
3456789012
4567890123
5678901234
6789012345
7890123456
8901234567
9012345678
Then you should be able read it in with something like:
Code:
#include <stdio.h>

int main(){
    int nSodukuArray[9][9];
    FILE* fin;
    int i, j, ch;
    if(!(fin=fopen("soduku.in","r"))){
        printf("Error opening file\n");
        return -1;
    }
    for(i=0; i < 9 && !feof(fin); ++i){
        for(j=0; j < 9 && !feof(fin); ++j){
            nSodukuArray[i][j]=fgetc(fin);
            if(nSodukuArray[i][j] == '\n'){
                printf("Error reading from file\n");
                return -1;
            }
        }
        ch=fgetc(fin); /* discard '\n' on the line */
    }
    fclose(fin);
    /* do what ever with the 2d array */
    return 0;
}
Only problem here might be that you get your array mirrored since your columns now appears to be located as rows...
But when doing calculations and displaying that cen be rearranged.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote