Thread: Reading into an array.

  1. #1
    Unregistered
    Guest

    Reading into an array.

    I'm having a brain cramp as I haven't programmed in C in over a year and am having issues. How would I read into an array?

    I.E. I have this:

    for (i=0; i<ROWS; i++){
    for (j=0;j<COLS;j++){
    fscanf(file, "16%d", &array[ ][i][j]);
    }
    }

    What I'd like is for the 16 integer elements on that line to become elements 1-16 for the first dimension of the array, so how would I do that?

    I can't believe I can't remember this..

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Well, you know, it goes something like:
    Code:
    int main()
    {
       const int rows = 2;
       const int columns = 3;
       FILE *fptr = fopen(filepath,"r");
    
       int array[rows][columns];
       for(int i = 0; i < rows; i++)
       {
          for(int ii = 0; ii < columns; ii++)
          {
              fscanf(fptr, "%d", &array[i][ii]);
          }
       }
       return 0;
    }

  3. #3
    Unregistered
    Guest
    >What I'd like is for the 16 integer elements on that line to become elements 1-16 for the first dimension of the array, so how would I do that?
    Code:
    int array[16][2] = {0};
    
    for(i = 0; i < 16; ++i)
      fscanf(file, "%d", &array[i][0]);
    Result in array:
    1 0 0
    2 0 0
    3 0 0
    4 0 0
    5 0 0
    .
    .
    16 0 0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading data into a 2D array
    By swgh in forum C Programming
    Replies: 2
    Last Post: 08-17-2007, 03:07 AM
  2. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Reading a mixed Text and decimal file into an array
    By djamie in forum C Programming
    Replies: 3
    Last Post: 08-05-2003, 06:25 AM
  5. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM