fscanf for multi dimensional arrays

This is a discussion on fscanf for multi dimensional arrays within the C Programming forums, part of the General Programming Boards category; i am trying to take a line from a text file using fscanf the textfile looks like this: before and ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    28

    fscanf for multi dimensional arrays

    i am trying to take a line from a text file using fscanf
    the textfile looks like this:

    before and after % whiteny houston texas
    on the map % Lake Arrowhead California
    (etc for 46 lines)

    Code:
    FILE *inp;
    inp = (FILE *) fopen("clues.txt", "r");
    int row, column;
    char clue[row][column]
    for(row = 0; row <= 46; ++row)
    {
    for(column = 0; column <= 60; ++column)
    {
    fscanf(inp, "%s", &clue[i]);
    }
    }
    printf("%s", *clue); 
    }
    We need to get each line into a seperate line of the array right now it only prints one word and seems to only be putting one word from a random line into the array.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Code:
    int row, column;
    char clue[row][column]
    this is not a way to declare array - dimmentions should be constants

    &#37;s reads till the first whitespace
    %[^\n] reads till the newline
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    We have changed everything that you said and it now reads and prints the first line of the file but how would we get it to read all the lines into the array?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by rambos View Post
    We have changed everything that you said and it now reads and prints the first line of the file but how would we get it to read all the lines into the array?
    Using loop?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    25
    Code:
    FILE *inp;
    inp = (FILE *) fopen("clues.txt", "r");
    int row, column;
    char clue[row][column]
    for(row = 0; row <= 46; ++row)
    {
    for(column = 0; column <= 60; ++column)
    {
    fscanf(inp, "%s", &clue[i]);
    }
    }
    printf("%s", *clue); 
    }
    i, undecleared
    Code:
    # define EVERYTHINK 0
    int main ( void )
    { 
       while ( C_learning )
                 english_learning ( );
       return EVERYTHINK;
    }

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    1
    Whoops, Im working with rambos on this project and he just copied it wrong that i is actually "row".

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    Hmm.

    > FILE *inp;
    > inp = (FILE *) fopen("clues.txt", "r");

    There shouldn't be a reason to cast the return types of these functions unless you've neglected to include the necessary header file <stdio.h>. I'd just include that and not worry about the return type.

    > int row, column;
    > char clue[row][column]
    > for(row = 0; row <= 46; ++row)
    > {

    There's a couple of things weird with this declaration. It appears like you want to build an array of a specific size, but in fact you don't do that at all. You instead use row and column to run the loop.

    > for(column = 0; column <= 60; ++column)
    > {
    > fscanf(inp, "&#37;s", &clue[i]);
    > }
    > }

    That will never quite run the way it should. If you're going to loop through every row of every column you should be reading characters instead of strings.

    Apart from the syntax errors, these are the big things that worried me. Lemme see if I could guess at what you really wanted here:
    Code:
    #include <stdio.h>
    #include <stddef.h> /** for size_t **/
    
    /** example: **/
    
    int main (void)
    {
        char clues[40][60];
        size_t i = 0;
        FILE * inp = NULL;
    
        inp = fopen("clues.txt", "r");
    
        if (inp != NULL) {
           for (i = 0; i < sizeof clues / sizeof clues[0]; i++) {
              fscanf(inp, "%59s", clues[i]);
           }
           for (i = 0; i < sizeof clues / sizeof clues[0]; i++) {
              printf("%s\n", clues[i]);
           }
           fclose(inp);
        }
        return 0;
    }
    clues[i] is the same type of thing as &clues[i][0].
    Something like that ought to work okay, though I only got a compile. Testing it is your problem.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,756
    Note how citizen specifies the size to fscanf to avoid a buffer overrun. More information here:
    http://cpwiki.sourceforge.net/Buffer_overrun
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two Dimensional Arrays
    By PritinTheGreat in forum C++ Programming
    Replies: 2
    Last Post: 06-05-2008, 04:52 AM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  4. two dimensional arrays
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 05-01-2002, 08:12 PM
  5. Two dimensional arrays
    By Jax in forum C Programming
    Replies: 1
    Last Post: 11-07-2001, 11:53 AM

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