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)
3
  • Have a look at stackoverflow.com/q/41195151 Commented Feb 22, 2022 at 18:02
  • 1
    Pleae provide a minimal reproducible example which demonstrates the undesired behaviour with specified sample input. Commented Feb 22, 2022 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). Commented Feb 22, 2022 at 18:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

"fgets() will also set errno to indicate the error" -- That is a requirement of POSIX, but ISO C does not require this.
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-ReinstateMonica I have rephrased , but is what you state correct according to this?
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? and fgets(). IAC, errno is not needed to discern the result of fgets().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.