I have a program that either takes data from a file or from the standard input.
I wrote code for scanning the file using sscanf.
I was wondering if I could reuse that code but with stdin instead of using scanf?
Ex:
How could I modify this so that it works with standard input?
while(fgets(buffer, MAX_LEN, input) != NULL) {
if (sscanf(buffer, "%s %s %s", one, two, three) == 3) { }
}
You don’t need to change the
sscanf()call – it’s thefgets()call that is reading from the input file.You can change your code snippet to work with
stdinby literally replacinginputwithstdinin thefgets()call (stdinis a globalFILE *declared instdio.hthat refers to standard input).