-2

I have the following C code in code.c

int main(int argc, char *argv[]) {
    FILE *openFile = NULL;
    openFile=stdin;
}

but when I compile and run my code with

gcc -g -o compiledcode code.c
./compiledcode

the terminal doesn't prompt me for an input. What is wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kitty
  • 7
  • 4
  • 2
    Is that all your code? –  Jan 30 '19 at 01:54
  • 1
    Welcome (back) to Stack Overflow. Please reread the [Ask] page, and even more urgently, read about how to create an MCVE ([MCVE]). What you've provided is too minimal to be useful. We can only assume that the second line is inside a function; the first might be inside or outside a function. There's not enough information to know what else is going wrong. – Jonathan Leffler Jan 30 '19 at 01:58
  • Yes, that is all my code inside main. I will edit it to show the main function now – Kitty Jan 30 '19 at 02:04
  • You aren't prompting the user for any input, nor are you trying to read any input. All you've done is set the file stream pointer `openFile` to point to standard input; you've never read anything (or written anything). I note in passing that since you are not using the command line arguments, you should write `int main(void)` in preference to `int main(int argc, char **argv)`. – Jonathan Leffler Jan 30 '19 at 02:06
  • Okay, so how can I ask for input using stdin? – Kitty Jan 30 '19 at 02:07
  • You can use `printf()` to print the prompt to standard output, and you could use `scanf()` or `fscanf()` or `getchar()` or `getc()` or any of the other input functions to get input. Please consult your textbook; this sort of stuff tends to be covered quite early. – Jonathan Leffler Jan 30 '19 at 02:09
  • For reference, I was following this post, which seems like it worked for them? https://stackoverflow.com/questions/3495092/read-from-file-or-stdin – Kitty Jan 30 '19 at 02:09
  • Don't have a textbook, got any recommendations? – Kitty Jan 30 '19 at 02:10
  • Were you following the question or the answer(s)? The question code fails, The accepted answer code uses `fgetc()` — one of the other input functions I didn't mention — to get data. – Jonathan Leffler Jan 30 '19 at 02:11
  • I'd recommend King [C Programming: A Modern Approach (2nd Edition)](http://knking.com/books/c2/index.html) from the books listed at [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). It seems to do a competent job. The classic is K&R, but that is now rather dated (the second edition describes C90, and was published just before the original C standard was finalized). It's still a good (compact) book, but it shows its age. – Jonathan Leffler Jan 30 '19 at 02:14
  • go it, thanks for the help! – Kitty Jan 30 '19 at 02:19

1 Answers1

1

You just open stdin as file but you don't read your file. There are many different ways to get input from stdin.

Function getchar can be used to read a single character from stdin. Use getc() or fgetc() to read from an arbitrary file stream. Example:

int c = getchar();
printf("you entered %c\n", c);

Function fgets can be used to read a line from file. Example:

char data[200];
fgets(data, sizeof(data), stdin); // we type stdin as file.
printf("you entered %s\n", data);

Function scanf and its family of functions can be used to read many different formats from stdin. example:

char data[200]; // size need be bigger or equal to input length
scanf("%199s", data);  // Protect from buffer overflow
printf("you entered %s\n", data);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    You should NEVER use gets. Use fgets() instead. Is a more safer function. –  Jan 30 '19 at 02:41