Tuesday 15 January 2013

c - fgets() getting stuck in infinite loop -


If I am using the fgets () function to search for a particular delimiter across a text file, So how can I ensure Fgets () does not loop infinitely on EOF?

I have the nth event of delimiter1 / delimiter2 pattern in struct1 [i] .string1 through delimiter2 from delimiter1, this pattern continues throughout the entire text file until the very end, Instead of being 2, there is EOF, I want to add everything from Limiter 1 to EOF.

  int i; (See the check) if the current line delimiter1 j = strcmp (temp_string, "delimiter2") (if the current line delimiter1 (= (fgets (temp_string, 100, inFile) = NULL) {if (strcmp (temp_string, "delimiter1") = ) is ; While (j! = 0 & amp; amp; temp_string! = NULL) {// I try to get out of here if it is EOF fgets (temp_string, 100, inFile); Strcat (struct1 [i] .string1, temp_string); J = strcmp (temp_string, "delimiter2"); // update comparator} i ++; }}}   

However, when I try to run this code, I get stuck in an infinite loop. I put a print statement in the interior, while the loop is showing what the integer "I" was, and it was stuck on number 4, which is the number of total delimiter 1 in the text file, I believe that the eof is giving me infinite loop.

Any help would be appreciated.

The reason for the infinite loop is the inner loop:

  ! = 0 & amp; amp; temp_string! = NULL) {// here ^^ never set up. Never, if "Delimiter 2" is not found   

Believe it, if the temp_string value does not have no "delimiter2" , then you Never j = 0 and you do not set the temp_string tap

if you read in temp_string 100 then " Delimiter2 ". can be read from the file with some other charter, which does not return even after reading the StackCamp () reason " Delimiter2 ".

Try to bug your code by using printf you temp_string

In addition, you can find "delimiter2" in your code for strstr () function can be used in place of strcmp () . strstr () returns a valid address if "delimiter 2" is found in any position in any position, other intelligent faucets

No comments:

Post a Comment