more help...2d arrays in structures - sscanf

This is a discussion on more help...2d arrays in structures - sscanf within the C Programming forums, part of the General Programming Boards category; I'm reading in this file: Code: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    24

    more help...2d arrays in structures - sscanf

    I'm reading in this file:

    Code:
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
    0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
    0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
    0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
    0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
    0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
    0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
    And i need to output it to my image.coords[][] 2d array. For example image.coords[0][k] would all be 0. image.coords[1][1] should be 3. Here's my code to do so, why is it outputting all 0's? I've been looking at it for a while, and the longer I look at it, the farther away I get to figuring it out. I'm guessing its some simple little error, but i can't figure it out.

    Code:
        k = j = 0;
        while( fgets(buffer, MAXLINE, in) )
        {
            k=i=0;
            while(1)
            {
                while(!isspace(buffer[i]))
                {
                    sscanf(buffer, "%d", &image.coords[j][k]);
                    k++;
                    i++;
                }
                while(isspace(buffer[i]))
                {
                    i++;
                }
                if(buffer[i]=='\0')
                {
                    break;
                }
    
                printf("%d ", image.coords[j][k]);
    
            }
    
            printf("\n");
            j++;
        }
    Last edited by mapunk; 12-01-2005 at 04:08 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you incrementing both row and column any time you find a number?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by quzah
    Why are you incrementing both row and column any time you find a number?


    Quzah.
    I'm incrementing the buffer (i) and the column (k) whenever I find a number. I increment the row (j) at the very end, when the first while loop has to go to the top again.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ah, right. I must have missed that due to your overly descriptive variable names. At any rate:
    Code:
    while(!isspace(buffer[i]))
                {
                    sscanf(buffer, "%d", &image.coords[j][k]);
    While we don't have a space at this particular character, read the first entry of the buffer over and over again into various spots in my 'coords' array.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by quzah
    While we don't have a space at this particular character, read the first entry of the buffer over and over again into various spots in my 'coords' array.
    Yeah that's my problem, I dunno how to assign say buffer[0], buffer[1], and buffer[2] to one array value instead of three. What should I do?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    57
    ...a hint?...
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) {
       int coords[7][24];
       char buffer[] = "0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0";
    
       char *pbuffer = buffer;
       int j,k;
    
          /* for row "j" */
          j = 3;
          for ( k=0 ; k<24 ; k++) {
             while(isspace(*pbuffer)) pbuffer++;
             sscanf(pbuffer, "%d", &coords[j][k]);
             while(isdigit(*pbuffer))  pbuffer++;
          }
    
          for ( k=0 ; k<24 ; k++) {
             printf(" %d",coords[j][k]);
          }
    
       return 0;
    }
    Last edited by cdave; 12-01-2005 at 05:25 PM.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    24
    Quote Originally Posted by cdave
    ...a hint?...
    no b/c i'm going to be reading in different files, so i can't initialize the array to anything

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to 2D arrays
    By bertazoid in forum C Programming
    Replies: 16
    Last Post: 02-26-2009, 04:46 PM
  2. Help with 2d Int Arrays
    By Voondebah in forum C Programming
    Replies: 2
    Last Post: 02-25-2009, 11:36 PM
  3. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM
  4. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 01:28 PM
  5. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM

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