pointer file to an array??

This is a discussion on pointer file to an array?? within the C Programming forums, part of the General Programming Boards category; so for my c class i have to write a program than scans a txt file of a sudoku grid ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    15

    pointer file to an array??

    so for my c class i have to write a program than scans a txt file of a sudoku grid and the program has to determine whether or not the grid follows the rules of sudoku. i have all of the program written but i can't figure out how to fscanf the array into the program....
    any suggestions?

  2. #2
    Registered User
    Join Date
    Dec 2005
    Location
    Colchester, Essex, United Kingdom.
    Posts
    31
    We need to know the format of the file with the sudoku puzzle in it. Also, if you could post any code you may have already tried, that would help.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    it's a .txt file...

    Code:
     #include <stdio.h>
    #include <math.h>
    int main(void)
    {
     
            /* Declare Variables */
            FILE *inp;      /* pointer to input files */
            int sudoku[9][9];
            int i,j;
            int count;
    
            /* Open sudoku grid */
            inp = fopen("sudoku-solution.txt", "r");
    
            for(i = 0; i<=9; ++i);
            count[i]=0;
            j=0;
            do(i = 0; i <= 9; ++i)
            fscanf("%d", &sudoku[i][j]);
            printf("%d", &sudoku[i][j]);
    
            fclose(inp);
    return(0);

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    when i try to compile, it says there is a syntax error before the ';' in line with the do statement

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Don't put a semicolon after a for loop. That just tells the loop to spin its wheels doing nothing. Also, the only loop with a do is a do/while loop. You have a do and no while, so that is incorrect.

    Furthermore you can't do count[i] because count isn't an array. You declared it as a plain integer. You are probably trying to do count = i, which is sort of useless to do every iteration.

    Try to think through exactly what it is that you're trying to do, and write it out step by step in English or another natural language. Then translate that into C bit by bit.
    Last edited by MacGyver; 03-29-2007 at 05:52 PM.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    Code:
            
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
     
            /* Declare Variables */
            FILE *inp;      /* pointer to input files */
            int sudoku[9][9];
            int i,j;
            int count;
    
            /* Open sudoku grid */
            inp = fopen("sudoku-solution.txt", "r");
    
            for(i = 0; i <= 9; ++i)
            fscanf("%d", &sudoku[i][j]);
            printf("%d", &sudoku[i][j]);
            
            fclose(inp);
    return(0);
    }
    hmm now when i try to compile it says

    sudoku-verifier.c:28: warning: passing arg 1 of `fscanf' from incompatible pointer type
    sudoku-verifier.c:28: warning: passing arg 2 of `fscanf' from incompatible pointer type

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You have to tell fscanf() which file to read from:

    Code:
    FILE *f;
    int x;
    ....
    
    /* Assume f points to an opened FILE and x has a valid number. */
    fscanf(f,"%d",&x)

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    Code:
    #include <stdio.h>
    #include <math.h>
    int main(void)
    {
    
            /* Declare Variables */
            FILE *inp;      /* pointer to input files */
            int sudoku[9][9];
            int i,j;
            int count;
    
            /* Open sudoku grid */
            inp = fopen("sudoku-solution.txt", "r");
    
            for(i = 0; i <= 9; ++i)
            fscanf(inp, "%d", &sudoku[i][j]);
            printf("%d", &sudoku[i][j]);
    
            fclose(inp);
    return(0);
    }
    it compiles now but instead of printing the grid it outputs "-4196600" and not the sudoku grid from the .txt file

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yes, that's because of a few reasons:

    1) You're ruining your array. The double array as dual indexes of 9 elements, so 81 in total. Are you trying to read all 81? What you're doing is reading 10 numbers, because you're starting at index 0 and working your way until index 9, inclusively.

    If you just want to read a one dimensional array, it's like this:

    Code:
    int i, array[9];
    for(i=0;i<9;i++)
    {
    	scanf("&#37;d",&array[i]);
    }
    For a two dimensional array, it's like this:

    Code:
    int i, j, array[9][9];
    for(i=0;i<9;i++)
    {
    	for(j=0;j<9;j++)
    	{
    		scanf("%d",&array[i][j]);
    	}
    }
    2) Your loop does not include the printf(). That is outside the loop because you neglected to put braces around both statements.

    3) You're printing the address of the element of the array in your printf() statement, not the value that the array contains at that index.
    Last edited by MacGyver; 03-29-2007 at 06:22 PM.

  10. #10
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    for(i = 0; i <= 9; ++i)
    { // You need to have braces if want to execute more that one command inside the loop
            fscanf(inp, "%d", &sudoku[i][j]); // you have to tell the compiler which file to read from
            printf("%d", &sudoku[i][j]);
    } // closing brace
    Don't quote me on that... ...seriously

  11. #11
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    so it gave me all of my grid but it listed it in one line. theres also a lot of extra numbers at the end of the grid and i dont know what they are. ideally, i should get a grid that looks like this:

    1 2 3 4 5 6 7 8 9
    4 5 6 7 8 9 1 2 3
    7 8 9 1 2 3 4 5 6
    2 3 4 5 6 7 8 9 1
    5 6 7 8 9 1 2 3 4
    8 9 1 2 3 4 5 6 7
    3 4 5 6 7 8 9 1 2
    6 7 8 9 1 2 3 4 5
    9 1 2 3 4 5 6 7 8

    Code:
    #include <stdio.h>
    int main(void)
    {
    
            /* Declare Variables */
            FILE *inp;      /* pointer to input files */
            int sudoku[9][9];
            int i,j;
    
            /* Open sudoku grid */
            inp = fopen("sudoku-solution.txt", "r");
    
            for(i = 0; i <= 9; ++i)
            {
                    for(j=0; j<=9; ++j)
                    {
                    fscanf(inp, "%d", &sudoku[i][j]);
                    printf("%d", sudoku[i][j]);
                    }             
            }
             
            fclose(inp);
    return(0);
    }
    //
                                                                                   
    
    kkeyser@shelltoe:~/CPrograms> ./a.out
    12345678945678912378912345623456789156789123489123456734567891267891234591234567800000-419660866884-12775696000-1336878000-4196608668841-41965080

  12. #12
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Why do you think it's printing all on one line? What is missing from your printf() statement?

    Perhaps a simple \n?

  13. #13
    Registered User
    Join Date
    Mar 2007
    Posts
    15
    i tried that and then it printed every single number in one column

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    So then add another printf() statement inside the first loop but after the second. Just have it print a newline char.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by snappleapple View Post
    //


    kkeyser@shelltoe:~/CPrograms> ./a.out
    12345678945678912378912345623456789156789123489123 456734567891267891234591234567800000-419660866884-12775696000-1336878000-4196608668841-41965080
    Think of the printout as rows and columns of your puzzle:

    Code:
    for (row = 1; row < 10; row++)  {
        putchar('\n');                              /* start each row on a new line */   
        for (col = 1; col < 10; col++)  
            printf(" &#37;d", puzzle[row][col]);
    }
    I didn't use the zero array element in my Sudoku program, because all the puzzle people talk of "row 1 --> row 9", and never row 0 --> row 8. Same with columns.

    The zero'th array element is zero'd out, and never used in any way. Makes it sweet imo.

    Adak

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  2. Dynamic pointer array in C
    By MacFromOK in forum Windows Programming
    Replies: 14
    Last Post: 04-09-2005, 07:14 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21