I'll preface this with the standard: I am very new to C programming so please be gentle.
I'm writing a C program that should be able to take a file path/filename as a command line arg, failing that, it should accept user input. I have the argv[1] filename working, but I don't know how to get it to switch to stdin if the user doesn't add a file name as an arg. The input should be raw data, not a filename. here's my (very newbie) code. As a new programmer I may need some extrapolation of explanation and I apologize in advance for this.
int main(int argc, char* argv[]) {
#ifndef NDEBUG
printf("DBG: argc = %d\n", argc);
for (int i = 1; i < argc; ++i)
printf("DBG: argv[%d] = \"%s\"\n", i, argv[i]);
#endif
FILE* stream = fopen(argv[1], "r");
char ch = 0;
size_t cline = 0;
char filename[MAX_FILE_NAME];
filename[MAX_FILE_NAME - 1] = 0;
if (argc == 2) {
stream = fopen(argv[1], "r");
if (stream == NULL) {
printf("error, <%s> ", argv[1]);
perror(" ");
return EXIT_FAILURE;
}
}
else if (argc ==1)
printf("Enter a list of whitespace-separated real numbers terminated by EOF or \'end\'\n");
//continue with program using user-input numbers