Solved

read array from text file

Posted on 2004-04-18
7
446 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 500 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now