Error1f1f
Error1f1f

Reputation: 539

Iterating through a directory of files and reading each line into an array

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.

Upvotes: 0

Views: 1605

Answers (1)

gnab
gnab

Reputation: 9571

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.

Upvotes: 1

Related Questions