Thread: Read a 2D array

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Read a 2D array

    I need to make a matrix[i][j] in C which will read from a file with the format bellow:

    1,3.567,4.893,1.756
    2,7.875,8.872,4.438
    3,8.234,9.987,3.908
    .
    .
    .

    so as the matrix[0][0]=1, matrix[0][2]=4.893, matrix[2][1]=8.234 and so on ...

    can anyone help me in how to allocate memory and read the file

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you KNOW that you always have three columns?

    If so, you could use realloc on a float/double [3] array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    The num of rows and columns will be different

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Then you need some way of determining how many columns there are, and use a 2D dynamic array - common question here, so search the forum for an answer. It's certainly a lot easier to solve this sort of problem if you know a max size that it may be, but I also understand that this is not always feasible.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read .txt file into 2D array.
    By crazygopedder in forum C Programming
    Replies: 11
    Last Post: 10-21-2008, 08:42 PM
  2. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  3. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM