reading from file to arrays

This is a discussion on reading from file to arrays within the C Programming forums, part of the General Programming Boards category; i wrote this programme to read from an input file into an array but the thing is i want to ...

  1. #1
    supernormal2
    Guest

    reading from file to arrays

    i wrote this programme to read from an input file into an array but the thing is i want to read each line in the input file into a different array where my programe reads every thing in the input file into one array....... can any one help with that

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define nrows 3
    #define ncols 5
    main(){
    
    int i,j;
    int taxi_location[nrows][ncols];
    FILE *data;
    
                
    data=fopen("inputdata.c","r");
    if (data==NULL){
            printf("Error: can't open input file\n");
            exit(1);
     }
     for(i=0;i<nrows;i++){
     for(j=0;j<ncols;j++){
      fscanf(data,"%d",&taxi_location[i][j]);
     }
    }
     //print array//
     printf("array is \n");
     for(i=0;i<nrows;i++){
     for(j=0;j<ncols;j++){
      printf("%d",taxi_location[i][j]); 
      }
      }
    }
    my input files contan

    1 2 3 4 5
    3 5 7 8 9
    1 3 4 6 7

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    On an array the outer loop is the nrows- hold this constant while loop through the ncols.

    Also, I would not name the file I am reading from- have a ".c" extension"- usually it is a .dat or .txt.
    Mr. C: Author and Instructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading char arrays from a binary file
    By eaane74 in forum C++ Programming
    Replies: 17
    Last Post: 11-20-2007, 04:19 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Reading from a .txt file or use arrays.
    By []--JOEMAN--[] in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2001, 07:55 AM

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