fgets() returns a string from a provided stream. One is stdin, which is the user input from the terminal. I know how to do this, but if I am using compiled c code in applications, this is not very useful. It would be more useful if I could get a string from a file. That would be cool. Apparently, there is some way to provide a text file as the stream. Suppose I have the following code:
#include <stdio.h>
int main(int argc, const char *argv){
char *TextFromFile[16];
fgets(TextFromFile, 16, /*stream that refers to txt file*/);
printf("%s\n", TextFromFile);
return 0;
}
Suppose there is a text file in the same directory as the c file. How can I make the printf() function print the contents of the text file?
char* x[16]
is an array of sixteen character pointers, not an array of sixteen characters. If this code compiles quietly you need to turn on more warnings because thatfgets
call is invalid. – tadmanargv[0]
passed tomain
.) – Eric Postpischil