LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Reading from a txt file into a two dimension array in C (http://www.linuxquestions.org/questions/programming-9/reading-from-a-txt-file-into-a-two-dimension-array-in-c-386800/)

kponenation 11-26-2005 06:35 PM

Reading from a txt file into a two dimension array in C
 
I'm trying to read from a txt file into a two dimension array. The text file contains following data.

Code:

0 0 0 1 0 0 1 0 1 0 0 1
1 0 1 0 1 0 1 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0
1 1 0 1 0 1 0 1 1 1 1 1

I want to create a two dimensional array (in this case 4X12 array) by reading the txt file.
Can anyone give me a hint on how to do this?

Thanks

kponenation 11-26-2005 07:07 PM

my code looks something
Code:

if (fp == NULL) exit(EXIT_FAILURE);
    for (i=0; i<r; i++)
        for (j=0; j<c; j++){
            fread(a[i][j], 1, c, fp);
                                }
                     
  }

of course it doesn't compile.

zahadumy 11-26-2005 07:30 PM

something like this
Code:

man fopen
man fgets
man fscanf
man fclose

These should help you. It's not al all difficult.

kponenation 11-26-2005 08:04 PM

I used fscanf to solve the problem...
Problem solved!!


All times are GMT -5. The time now is 06:48 PM.