I have a C program that reads from a txt file. The text file contains a list of words, one word per line. What I want to do is get the words from the text file and print them as one sentence/paragraph, however, when I try to print them, they still print one word per line.
I am storing the words in a 2d char array as I read them from the file. What I think is happening is the array copies the newline char from the txt file, is that correct? and if so, how do I add the word into the array without the new line char?
while(fgets(line,20,lineRead)!=NULL)
{
for(j = 0; j < 20;j++)
{
message[k][j]= line[j];
}
printf("%s", message[k]);
}
I tried a few while loops with no success:
while(line[j] != ' ')
while(line[j] != NULL)
while(line[j] != EOF)
while(line[j] != ' \')
I’m learning C so please be specific in my error. I want to understand what I’m doing wrong, not just get an answer.
Thank you!
You could simply change your for loop to be: