Hello! I'm trying to read from a text file that's being redirected to my program.
Code:
./program < test.txt
The code looks like this:
Code:
// read redirection
char buffer[50][200] = {0};
int size = 0;
while(fgets(buffer[size], 200, stdin) != NULL) {
printf("%s", buffer[size]);
size++;
}
printf("\nPrinting Array\n");
for(int i=0; i < size; i++) {
printf("%s", buffer[i]);
}
Is this the best way to do it? I feel like I have a lot of extra space/size because I'm not sure how long the commands or text file will be. Should I be reading the length of the text file somehow?
Additionally, I thought of setting the last element to NULL, but apparently I can't do something along the lines of
Code:
buffer[size+1] = NULL;
Why exactly does C stop me from doing this? And how would I mark the end of the buffer array?