0

I was wondering what the most efficient way of reading a directory of files line by line into an array was. My task is to search a directory for files that end with a certain extension and read each line of that file would should contain only integers on each line into a single dimension array. Then move onto the next file and add each line of that file to the existing array. I was looking at reading a text file into an array in c and was thinking I could modify Johannes' example to fit my scenario. Thanks in advance.

Edit:

Apologies guys when I said efficient I was in terms of memory allocation. Also it does have to be in c. I have added the homework tag and and shall research the man pages on glob. Will report back.

Community
  • 1
  • 1
Error1f1f
  • 529
  • 2
  • 9
  • 20
  • I don't quite understand the question. Are you scanning a directory hierarchy for files, or are you scanning text in a file? If it's the prior, then I'd suggest you have a look at the ''glob'' function ('man 3 glob'). If the latter, you're already on the right track (and might want to look into the ''strstr'' function among others). Further elaboration on what you actually want to do might yield more exact answers. – gamen Mar 12 '11 at 19:08
  • 2
    Most efficient in terms of programmer time, execution time, execution space, or what? "Most efficient" needs a little more clarification. – mpez0 Mar 13 '11 at 01:06

1 Answers1

1

If I am correct, the task is to collect the number present on each line in a set of files with some extension.

Iterating files can be done in multiple ways, as can matching file extensions. Same goes for adding an unknown number of elements to an array.

Here is a suggested solution based on my understanding of the problem:

#include <stdio.h>
#include <stdlib.h>
#include <glob.h>

int main(int argc, char *argv[])
{
  char pattern[] = "/tmp/*.txt";
  glob_t globbuf;
  FILE *file;
  int i, num, n = 0, max = 10;
  int *numbers = malloc(sizeof(int) * max);

  glob(pattern, 0, NULL, &globbuf);

  for (i = 0; i < globbuf.gl_pathc; i++)
  {
    if ((file = fopen(globbuf.gl_pathv[i], "r")) != NULL)
    {
      while (fscanf(file, "%d\n", &num) == 1)
      {
        numbers[n] = num;
        n++;

        if (n >= max)
        {
          max = max * 2;
          numbers = realloc(numbers, sizeof(int) * max);
        }
      }
      fclose(file);
    }
  }

  globfree(&globbuf);

  printf("Collected numbers:\n");

  for (i = 0; i < n; i++)
  {
    printf("%d\n", numbers[i]);
  }

  free(numbers);

  return 0;
}

Files are located and matched for the correct extensions in a single operation, using the glob function. Numbers are collected in a dynamically allocated array, whose size is doubled, using realloc, every time you run out of space.

gnab
  • 9,251
  • 1
  • 21
  • 13