2-d arrays and fscanf

This is a discussion on 2-d arrays and fscanf within the C Programming forums, part of the General Programming Boards category; alright, i have absolutly no idea what im doing here, as the concepts of pointers, dynamic memory and 2-d arrays ...

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

    2-d arrays and fscanf

    alright, i have absolutly no idea what im doing here, as the concepts of pointers, dynamic memory and 2-d arrays are all new things to me

    i have a file that i need to read into a 2-d array...in the end that array needs to be dynamically allocated, but for now, lets just say it is a static 10x10 array.

    i can read in a single string just fine, but i cant seem to figure out how to read in the data to the multi dimentional array.

    the input file is formated like this:

    4
    0 100
    1 .004
    2 54

    where the first line is how many lines the file is, the first number of each line represents the line number and the second number in each line represents the data that i need to read in.

    any help at all would be apprecated....

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to print each element of a single dimension array? If not, start there.
    Do you know how to read into each element of a single dimensaion array? If not, start there.
    Do you know how to print each element of a single dimension array? If not, start there.

    Post your attempt of whatever stumps you, and we'll lend a hand.

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

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    yes, i can print each element of a single dimension array
    Code:
     
    int x[10] = "adsfad";
    
    
    printf("%c\n",x[3]);
    and this is my best attempt at reading in a single element at a time

    Code:
    FILE *input;
    int array[10];
    int i;
    
    myinput = fopen("file","r");
    
    for(i=0;i<11;i++){
         fscanf(input,"%c",array[i]);
    }
    im pretty comfortable working with one-d strings....its just when moving into tow dimensions things seem to get tricky.

    just an overall idea of whats going on is what im looking for...i want to write this myself, but ive tried everything i can think of, so i came here for help.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    myinput should be input....my bad, im getting the variables im using in the project confused with the variables i wanted to use online.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    and it should be %i not %c.

    sorry...im trying to frantically write code and get help from about a million different sources at once.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Do you know how to display the contents of a two dimensional array?
    Code:
    int array[ 3 ][ 2 ] =
    {
        { 1, 2 },
        { 3, 4 },
        { 5, 6 }
    };
    
    int y, x;
    
    for( y = 0; y < 3; y++ )  /* each row... */
    {
        for( x = 0; x < 2; x++ ) /* each column... */
        {
            printf("array[ %d ][ %d ] is %d\n", y, x, array[ y ][ x ] );
        }
    }
    Now see if you can either write one of your own to do something similar, with a different number of rows and columns, for practice; or try and modify this to read into each part of the array.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using FSCANF to read mixed text/double files
    By cfdprogrammer in forum C Programming
    Replies: 3
    Last Post: 03-23-2009, 04:18 AM
  2. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 10:40 AM
  3. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 04:26 AM
  4. Replies: 11
    Last Post: 11-26-2004, 08:13 PM
  5. Assigning values to arrays using fscanf
    By Hallu in forum C Programming
    Replies: 5
    Last Post: 03-24-2004, 05:50 PM

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