Reading file into a 2d array

This is a discussion on Reading file into a 2d array within the C Programming forums, part of the General Programming Boards category; I wonder could anybody help me reading in a file to a 2d array File rates.txt contains: 1.00,0.99,0.64,121.42,4.65 0,1,2,3,4 I ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    7

    Reading file into a 2d array

    I wonder could anybody help me reading in a file to a 2d array
    File rates.txt contains:
    1.00,0.99,0.64,121.42,4.65
    0,1,2,3,4

    I am new to c so any help would be much appreciated.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What have you tried so far? Post some code and someone will help.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    It's the same as reading to a 1d array...
    File >> array[i][j];
    Where File in the file you have opened for input, i and j are integers that are less that the array size( if that's what you mean )

    And every thing else depends on the format of the input file, you have to know the format then, try to read from it.
    none...

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    File >> array[i][j];
    Looks a bit too C++ like for the C board !
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    As you can see I don't really have a clue how to read in the file. Do you need to use fscanf twice for each line in the file?
    Anyhow below is my miserable attempt:


    <code>
    #include <stdio.h>

    FILE *fp;


    int main(void)

    {

    float array[5][5];
    int i,j;

    if ((fp=fopen("rates.txt", "w+")))
    {
    fprintf(stderr,"cannot open %s\n","fp");
    }
    while(feof(fp)==0)
    {

    fscanf(fp, "%f,%f,%f,%f,%f\n%d,%d,%d,%d,%d", array[i][j]);

    for (i=0; i < 5; i=i+1)
    for (j=0; j < 5; j=j+1)
    printf("\n array[i][j]");
    }

    return 0;
    }
    </code>

  6. #6
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by Hammer
    File >> array[i][j];
    Looks a bit too C++ like for the C board !
    Sorry, you are right...
    none...

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    7
    What seems to be happening is its only printing
    array[i][j] all the ways down the screen.
    I want it to print the contents of the file:
    e.g

    0,1.00
    1,0.99
    etc
    etc
    etc

    any ideas?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>any ideas?
    Yes, read up on how to use printf() properly.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file into an array for fast access later
    By matsharp in forum C Programming
    Replies: 10
    Last Post: 08-03-2006, 03:42 AM
  2. reading from file to an array - help needed!
    By timeforheroes in forum C Programming
    Replies: 2
    Last Post: 04-28-2005, 01:16 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM
  4. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

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