Thread: how do you input data from a file into an array?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    19

    how do you input data from a file into an array?

    how do you take data from a file and inputted into an array if the data in the file were to look like this

    1 1 1
    2 1 1
    3 1 1
    4 1 1
    5 1 1
    6 1 1

    ...and
    (column 1) index of the array, i -- an integer;
    (column 2) value of element iin array x, x[i] -- a double value
    (column 3) value of element i in array y, y[i]


    so i for example when i do my first loop i should get this

    x[1]=1 y[1]=1

    and my second loop

    x[2]=1 y[2]=1.


    i know how to open file and work with loops, but i dont know how to make my first column the index of the array, and my second column the value of the element and so on. so any help would be appreciated.


    so it should look something like this


    Code:
    #include <stdio.h>
    int main()
    {
            FILE *inp;
            int input_status;
            int n;
    
            inp = fopen("xy.txt", "r"); //opens files
            input_status = fscanf(inp, "&#37;d", &n); //takes digits
    
            while (input_status != EOF) //loop repeats till end of file
            {
                    printf( "%d\n",n);// instead of this i need something that assigns it to i, and to the value of the element in array x, x[i]
                    input_status = fscanf(inp, "%d", &n);
                    }
                    fclose(inp);
    
    }
    Last edited by jorgejags; 11-03-2008 at 11:57 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by jorgejags View Post
    how do you take data from a file and inputted into an array if the data in the file were to look like this

    1 1 1
    2 1 1
    3 1 1
    4 1 1
    5 1 1
    6 1 1
    Are the 1...6 line numbers that you put in or are those present in the file along with the array data?

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    yeah those are the index of the array.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    5
    I guess you have to use the fscanf function twice. First you read the index number, then the two values.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    19
    thanks.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You can read each input line with a single fscanf() and do the assignments in the next step as in
    Code:
    #include <stdio.h>
    int main(void)
    {
            int x[10], y[10];
            FILE *inp;
            int ret, l, m, n;
    
            inp = fopen("file.txt", "r");
            while ((ret = fscanf(inp, "%d%d%d", &n, &m, &l)) != EOF && ret == 3) {
                x[n-1] = l;
                y[n-1] = m;
                printf("n = %d\nx[%d] = %d\ny[%d] = %d\n", n, n-1, m, n-1, l);
            }
            fclose(inp);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM