I'm trying to add a sentence to a text file but I seem to only add one word of the sentence instead of the whole sentence. I know that using fputs("this is a sentence", pFileText); It works just fine with adding text, but not by adding a string variable. What am I doing wrong?
char sentence[1000];
FILE * pFileText;
pFileText = fopen("text.txt", "a");
printf("Enter text: ");
scanf("%s", &sentence[1000]);
fputs("\n", pFileText);
fputs(sentence, pFileText);
fclose(pFileText);
&sentence[1000]is a pointer to one element beyond the end of the array.scanf("%s", ...)will read only one word. You can read entire lines (up to and including the terminating new-line character) withfgets().