read from the file into a 2d array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xxzl
    New Member
    • May 2015
    • 4

    read from the file into a 2d array

    how to read the file into 2d array without knowing its size?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, read this: http://bytes.com/topic/c/insights/77...rrays-revealed. Here you will learn that there really are not 2D arrays in C or C++.

    Second, find out the format the file.

    Third, count the numbers in the file.

    Fourth, dynamically allocate memory to hold those numbers. That is, use malloc() if you write in C or the new operator if you write in C++.

    Fifth, read the numbers from the file into your allocated memory

    Sixth, create a pointer to an array[x][y] where y is the columns and x is the number of rows. Place the address of your allocation in this pointer.

    Seventh, access your array only through this pointer. Your view will be a 2D array.

    Comment

    Working...