Solved

read array from text file

Posted on 2004-04-18
7
448 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
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.

813 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

15 Experts available now in Live!

Get 1:1 Help Now