Sarah Kazi
Sarah Kazi

Reputation: 71

how to read multiple files from a 2d array in C

i have to write a program that reads multiple files in a for loop and counts the words, sentences, and syllables of that file. My code in the function CountWords looks like this: (filenames is a 2d array with the names of the 17 files stored in it)

for (i = 0; i < 17; ++i) {
    inputfile = fopen(filenames[i], "r");
    if (inputfile == NULL) {

        printf("File cannot be found, verify %s is in the correct location.\n", filenames[i]);
    }
    int c;

    while (c != EOF) {

        c = getc(inputfile);
        //code to count words/syllables/sentences

    }
    fclose(inputfile);

While it successfully counts it for the file in the 0th element, the rest of them come out as zero words, zero sentences, zero syllables. Is there something I am doing wrong? thank you for your insight

Upvotes: 1

Views: 142

Answers (2)

Hurlu
Hurlu

Reputation: 11

I can see two problems in the code you showed us here :

First, the way you cycle through your file names is hazardous, as you could very well run into a part of the 2D array that isn't initialized. Prefer filenames[i] != NULL as a stopping condition, and make sure the array is correctly initialized:

for (i = 0; filenames[i] != NULL; ++i) {
    CountWords(filenames[i]);
}

Second, and that's the most important point, you are not checking your system call to fopen. Which means, your fscanf could very well be reading from a NULL pointer (what fopen returns in case the open failed).

If you get problems with your loops, try using printf to check your index, your fd, or whatever it is that could cause you problems; it is a good practice that will be useful to you in future debugging ;)

Upvotes: 1

Chunko
Chunko

Reputation: 352

You might try 'getc' rather than fscanf for reading single characters (if you're not reading UTF-8?) however I don't think that's the issue in this case.

If your current code is stopping after 2 files, perhaps the file doesn't exist or is not readable, in which case I believe the fopen would return null and set an error value in errno. Your code (unless you've simplified it for the post) doesn't check for a failure result from the fopen, so it might crash with a segfault at that point.

To find out what's going on I'd suggest checking that all the filenames exist and are readable, and in the code, add a check for the value of pInputFile

it's been a long time since I used C but something like this:

#include <errno.h>
pInputFile = fopen(inputFile,"r');
if(!pInputFile)
{
  fprintf(STDERR, "Failed opening %s [error %d]",inputFile,errno());
  return -1;
}

and then in your loop, check for the return value < 0 eg:

int count=0;
for (i = 0; i < 18; ++i) {
      count =  CountWords(filenames[i]);
      if(count < 0)
      {
        //discard the result
      }
      else
      {
        // do something with the result
      }
    }

good luck

and as Hurlu pointed out, you've hard-coded the number of files in the loop as 18. perhaps that doesn't match the size of your array. the usual convention in C would be to also pass to your function a number of items that are in the array, and use that in your for loop instead of the fixed number '18'

Also if your array of filenames is being populated by some other function, it could have put an invalid name or null value into the array.

Upvotes: 1

Related Questions