What seems to be the problem with this part of my code:
while(c != EOF){
c = fgetc(myFile);
p[i++]=c;
printf("%c", p[i]);
}
It does not seem to store the values in p[i] even though malloc succeeds, and prints garbage. However this code prints the characters fine:
while(c != EOF){
c = fgetc(myFile);
//p[i++]=c;
printf("%c", c);
}
p is a char* and i is initially 0.
Asked in the question Not getting all characters after reading from file
What is the problem here?
The line
p[i++] = cstores the character and incrementsi. The next line printsp[i]but i has already been incremented. Try this:As a side note, you could probably rewrite it