Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

read array from text file

Posted on 2004-04-18
7
Medium Priority
?
458 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:acsell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 2000 total points
ID: 10854720
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
 
LVL 17

Expert Comment

by:rstaveley
ID: 10854728
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
 
LVL 5

Author Comment

by:acsell
ID: 10854957
thank you very much for that,

what do I call the file with the array in?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:ankuratvb
ID: 10856827
You could use fscanf(stream,"%s %d",str,&num);
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10856841
>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
 
LVL 17

Expert Comment

by:rstaveley
ID: 10857341
> 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
 
LVL 5

Author Comment

by:acsell
ID: 10860927
Thanks :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question