Read more about C standard IO. Notice that gets
was deprecated and dangereous but is now removed from the C standard. You should never use it (and please, forget that it has existed). Probably you mean fgets(str, sizeof(str), stdin)
instead of your call to the disappeared gets
, assuming that str
is some array of char
s, e.g. declared as char str[100];
.
You might read an entire line with fgets
(or even getline(3), see this) then parse that buffer, perhaps using sscanf (or strtol, or strtok, or using your own lexing & parsing techniques).
Notice that sscanf
(like fscanf
and scanf
) returns a scanned item count (and accepts %n
) but don't care about end-of-lines. You need to use that.
Read documentation of the functions you are using very carefully.
When reading from a genuine file, you could also reposition inside it using fseek (with ftell). That won't work on the console (since the terminal is generally non-seekable).
Perhaps you could use some terminal related libraries (such as ncurses or readline). These are not in the C11 standard n1570 and might not be available on your operating system (but on Linux and some others OSes, you have them).
Remember that your stdin is not always a terminal. Think of redirections and pipelines.
Read also How to Debug Small Programs. Be sure to compile your code with all warnings and debug info (so gcc -Wall -Wextra -g
if using GCC).
If you are on Linux or some POSIX operating system, be aware that stdin is related to the file descriptor 0 (named STDIN_FILENO
). So if you want to wait till some input is available, consider using poll(2) on that file descriptor.
PS. Your question is really unclear. I can just guess what you want to do.... Consider improving it after having read more documentation...