0

If you are confused with "Array of strings" it is basically char arr[10][10];. I'm trying to type out an array of strings using 'for' loop, and the length of that array of strings is unknown (it's variable). How do I write a 'for' loop that would know where is the end of that array of strings and stop writing? (btw if you're curious as to what I'm needing, it is for a system that reads strings from a text file line by line and puts them in an array of strings) I can however detect EOF, so right now I'm using a for loop that puts lines in an array of strings, and immediately prints it... here it is:

fp = fopen ("file.txt","r");
for(i=0;!feof(fp);i++)
{
    fgets(array[i],20,fp);
    printf("\n%s",array[i]);
}
fclose(fp)
Laurel
  • 5,965
  • 14
  • 31
  • 57
Silk.Road
  • 1
  • 1
  • Have a look at https://stackoverflow.com/q/41195151 – Robert Harvey Feb 22 '22 at 18:02
  • 1
    Pleae provide a [mre] which demonstrates the undesired behaviour with specified sample input. – Yunnosch Feb 22 '22 at 18:04
  • If `array` is an actual 2-D array, you can determine the number of rows by `sizeof array / sizeof array[0]`. If `array` is actually a pointer to the first row of a 2-D array (e.g. from a function parameter) then it needs to be told how many rows there are (e.g. by another function parameter). – Ian Abbott Feb 22 '22 at 18:12

1 Answers1

2

Just check the return value of fgets() and keep a counter, when you get a NULL pointer, there is (probably) nothing more to read from the file.

   char array[10][10];
   FILE *fp = fopen ("file.txt","r");
   size_t max_size = sizeof(array) / sizeof(array[0]);
   size_t length = 0;


    while (length < max_size && fgets(array[length], sizeof(array[0]), fp) != NULL)
    {
        length++;
    }

   printf("%zu\n", length);
   fclose(fp);

But note that you might go out of bounds if it happens there are more than 10 lines in the file. You should also check that length is within the bounds of the array.

fgets() will also set errno to indicate the error, if you are on a POSIX-like system.

Also, see Why is “while ( !feof (file) )” always wrong and Why it's bad to use feof() to control a loop.

alex01011
  • 1,670
  • 2
  • 5
  • 17
  • 1
    **"fgets() will also set errno to indicate the error"** -- That is a requirement of POSIX, but ISO C does not require this. – Andreas Wenzel Feb 22 '22 at 21:34
  • alex01011, Can't up-vote answer with "fgets() will also set errno to indicate the error." as that is not what the C standard specifies. – chux - Reinstate Monica Feb 23 '22 at 01:52
  • @chux-ReinstateMonica I have rephrased , but is what you state correct according to [this](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fgets.html)? – alex01011 Feb 23 '22 at 16:23
  • Yes I am correct per the C spec. Your reference is not the C spec. See [Where do I find the current C or C++ standard documents?](https://stackoverflow.com/q/81656/2410359) and [fgets()](http://port70.net/~nsz/c/c11/n1570.html#7.21.7.2). IAC, `errno` is not needed to discern the result of `fgets()`. – chux - Reinstate Monica Feb 23 '22 at 16:49