first time posting here. I have looked at a few other peoples ways to do this, and one of the ways was nearly exactly the same way that I am trying to do. But, it doesn't work for me?
#include<stdio.h>
int main()
{
FILE *file;
char buffer[15];
char *text[12];
file = fopen("Text.txt", "r");
if(!file) {
printf("Failed");
return 1;
}
int count = 0;
while(fgets(buffer,sizeof buffer, file) != NULL) {
printf("%s", buffer);
text[count] = buffer;
count++;
}
printf("\n");
for (count=0;count<10;count++) {
printf("%s\n", text[count]);
}
fclose(file);
return 0;
}
Now on another site (while looking for a solution or help I found this http://www.daniweb.com/software-development/c/threads/316766/storing-string-in-a-array-reading-from-text-file
Where the person has done it the same way as me (apart from obviously slight differences in what they're reading etc).
My text file reads: The Quick Brown Fox Jumps Over The Lazy Dog (all on their own lines).
Basically I want to read the file in line by line and save each line in the next space in the array.
In the code when I use the line printf("%s", buffer); It prints out each word of each line okay to the console window. However, when I print out the contents of the array with the for loop it simply prints out "dog, dog, dog..." for each space in the array.
Can someone help me here? Am I doing it wrong?