• C

read array from text file

Hello, can someone please tell me or give me the address of a website that can tell me how to read a 10 by 10 array from a text file and how the numbers in the text file need to be layed out to read in properly.

i would also like 10 names followed by 10 numbers to be read from a text file but it depends how complicated this would be.

Once it is read from the text file, can it be treated like a normal array e.g. to print it out, I would just use-

printf([row][column])

Thanks, acsell
LVL 5
acsellAsked:
Who is Participating?
 
rstaveleyConnect With a Mentor Commented:
Here's how I suggest you do the 10x10 numbers:

(1) Data file uses newlines for separate rows and whitespace to separate columns:
--------8<--------
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
--------8<--------

(2) C code reads the file row by row (max 10 rows) and reads a maximum of 10 columns from each row - any nonexistent data is assumed to be zero:
--------8<--------
#include <stdio.h>

int main(int argc,char **argv)
{
FILE *fin;
char buf[1024];
double numbers[10][10];
int row,column;

        if (argc != 2 || (fin = fopen(*++argv,"rt")) == NULL)
                return fprintf(stderr,"Unable to open input file\n"),1;

        memset(numbers,'\0',sizeof(numbers));
        for (row = 0;row < 10 && fgets(buf,sizeof(buf),fin) != NULL;++row)
                printf("Row %d: Read %d values\n",row,sscanf(buf,"%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf"
                        ,&numbers[row][0]
                        ,&numbers[row][1]
                        ,&numbers[row][2]
                        ,&numbers[row][3]
                        ,&numbers[row][4]
                        ,&numbers[row][5]
                        ,&numbers[row][6]
                        ,&numbers[row][7]
                        ,&numbers[row][8]
                        ,&numbers[row][9]
                        ));
        fin = (fclose(fin),NULL);

        for (row = 0;row < 10;++row)
                for (column = 0;column < 10;++column)
                        printf("Row %d column %d: %lf\n",row,column,numbers[row][column]);
}
--------8<--------
0
 
rstaveleyCommented:
When it comes to reading names followed by numbers, you can use a similar approach if names do not include whitespace, using %s to read names strings into char arrays. If you need to support names with spaces in them, you need to do something a bit more clever with respect to looking for delimiters.
0
 
acsellAuthor Commented:
thank you very much for that,

what do I call the file with the array in?
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
ankuratvbCommented:
You could use fscanf(stream,"%s %d",str,&num);
0
 
ankuratvbCommented:
>what do I call the file with the array in?

Name it anything you want.At the time of executing the program,give the filename as the first command line argument.
0
 
rstaveleyCommented:
> what do I call the file with the array in?

ankuratvb is right, it is the first command line argument. If you want to hard-code the filename as (say) x.txt, replace

>         if (argc != 2 || (fin = fopen(*++argv,"rt")) == NULL)
>                return fprintf(stderr,"Unable to open input file\n"),1;

...with...

          if ((fin = fopen("x.txt","rt")) == NULL)
                 return fprintf(stderr,"Unable to open input file\n"),1;
0
 
acsellAuthor Commented:
Thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.