I can spot following problems:
-
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
isNULL
andrewind(NULL)
is undefined behaviour, most likely you’ll get a crash. -
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. -
fgetc
returns anint
, not achar
, therefore you should haveint chr;
. -
if(buffer[z] != "0")
is alwaysfalse
. For comparing strings you needstrcmp
. -
And finally:
max_chrcount
contains the maximum word length which is computed correctly, but you need one byte more to store theNUL
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]