Re: Circle [Code] (files)

From: Daniel Koepke (dkoepke@california.com)
Date: 02/18/97


On Tue, 18 Feb 1997, Reza Nejad wrote:

> Okay fellas (Daniel :))
> 
>  I'm having some troubles. I need to figure this out:
> 
> 
> I have a really big text files to be read in:
> 
> 
> example:
> 
> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
> 
> This goes on to right for 1000 columns and down for 1000 rows.
> 
> I'm trying to read this from a text file into an array:
> 
>  a[1000][1000]
> 
> 
> I know how to open the file
> and do everything needed except to be
> able to load this massive table into this array a.
> 
> Could anyone give me a hand? (Daniel :))

Well, it'd be possible to read the file one line at a time, then
split it up using one_argument. Probably not too efficient, but
it'll work. Look at the get_line() function and how CircleMud uses
it (it's probably in db.c). It should return how far you've
advanced. The only problem I can see is that if you have 2 characters
(the number and the space) 1000 times, that'd require you to change
get_line(), as it only reads 256 characters. Then, there's probably a
good reason for it, etc.

So, the other alternative (which is probably just as inefficient) is
to read the aual numbers. Like:

  void read_array(void) {
    int array[1000][1000], row = -1, col = 0;
    FILE *fp;
    char c;

    /* put fopen call here to open up the file, or if you want to
     * have this work on an already opened file, add a FILE *fp
     * parameter and remove the cast from inside this function */

    for (c = fgetc(fp); !feof(fp) && c != '~'; c = fgetc(fp)) {
      if (!isdigit(c))
        continue;
      if ((row++) > 999) {
        row = 0;
        if ((col++) > 999)
          break;
      }
      // this bit below converts 'c' to a number
      array[row][col] = c - '0';
    }
    fclose(fp);
  }

Now, as with everything, there is a downside to this. First, it's
written in my mailer and off the top of my head, so I don't know if
it's right. Second, it only supports single digit numbers. It won't
take too much redesigning to make it support longer than that. You
just need to make it keep looping for as long as it's a number before
it moves on to the next row/col. To do the places, you multiply the
previous number by ten before adding. Eg.,

  array[row][col] = array[row][col] * 10 + (c - '0');

The redesigning is left as an excercise to the reader.


--
Daniel Koepke
dkoepke@california.com
Forgive me father, for I am sin.


+-----------------------------------------------------------+
| Ensure that you have read the CircleMUD Mailing List FAQ: |
|   http://cspo.queensu.ca/~fletcher/Circle/list_faq.html   |
|    Or send 'info circle' to majordomo@cspo.queensu.ca     |
+-----------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/18/00 PST