[Solved] i want to copy strings from file into a variable 2d char [closed]


I can spot following problems:

  1. The first rewind(fp); is useless and wrong. Useless because after opening the file, the file pointer it is already at the beginning, and wrong because if the file could not be opened for some reason, fp is NULL and rewind(NULL) is undefined behaviour, most likely you’ll get a crash.

  2. The computing of word_count is wrong, because you simply count the number of spaces, which is one less than the number of words, unless the file ends with at least one space: Example: "One two three": two spaces here but three words.

  3. fgetc returns an int, not a char, therefore you should have int chr;.

  4. if(buffer[z] != "0") is always false. For comparing strings you need strcmp.

  5. And finally: max_chrcount contains the maximum word length which is computed correctly, but you need one byte more to store the NUL terminator, therefore you need this:


char buff[max_chrcount + 1];
char buffer[word_count][max_chrcount + 1];

However there are most likely more problems.

nd I’m not quite sure what the purpose of strcpy(buffer[b],"0") is. Did you mean buffer[b][0] = 0 ?

solved i want to copy strings from file into a variable 2d char [closed]