PDA

View Full Version : how can i read file on standard input using scanf?


7imz
06-08-2004, 04:28 AM
well i'm totally new to C... how can i read file on standard input using the scanf fuction? thanks

maharg
06-08-2004, 04:29 AM
Um... are you asking how to use scanf? Because scanf always reads from stdin.

7imz
06-08-2004, 04:31 AM
no i mean.. if i give it a file, say text.txt... how can i read the contents of that file?

7imz
06-08-2004, 04:33 AM
hmmm... nevermind.. i think i misunderstood what was being asked...

maharg
06-08-2004, 04:33 AM
Run it like this:
myprog < file.txt

If you want your program to open the file, use fopen() to open it, then use fscanf instead of scanf.

7imz
06-08-2004, 04:34 AM
thanks a bunch

Lathentar
06-08-2004, 04:50 AM
Or you could use C++ and use streams.

maharg
06-08-2004, 05:10 AM
Or you could use C++ and use streams.

In all my years of helping people with this sort of stuff I've learned it's generally best to let people burn their hands on the ring.

Lathentar
06-08-2004, 05:16 AM
In all my years of helping people with this sort of stuff I've learned it's generally best to let people burn their hands on the ring.

It took a generics programming class for me to realize the true power of streams. Might as well get him started on the right foot, its really easy to get attached to scanf/printf.

7imz
06-08-2004, 05:21 AM
it's a C course... i can't use anything but C :(