views:

317

answers:

4

Hello, I got a program with a fscanf like this:

fscanf(stdin, "%d %d,....

I got many fscanf and files that I'd like to test, the files are like this

10485770 15 51200000 -2 10 10 10485760 10485760 10 10485760 10485760 10 10485760 10485760

Well my question is how can I tell to the program or the compiler to take the inputs not from the keyboard, but from those files. These programs are benchmarks and in the files I got the inputs, I'm sure there is a way to do this automatic because in some case there are many inputs. Thank you in advance.

+1  A: 

Try freopen. Eg.

freopen( "somefile.txt", "r", stdin );
+3  A: 

When running from the command line, you can redirect a file to standard input using the '<' operator.

For example, on windows:

$ type input_file
10485770 15 51200000 -2 10 10 10485760 10485760 10 10485760 10485760 10 10485760
10485760
$ my_program.exe < input_file

Or on *nix:

$ cat input_file
10485770 15 51200000 -2 10 10 10485760 10485760 10 10485760 10485760 10 10485760
10485760
$ ./my_program < input_file
Sufian
A: 

Thanks for your answers, problem solved with the answer of Sufian, the redirection of the file to the standard input using < works perfect. Thank you so much.

If you like Sufian's answer, please click "Accept Answer" on it.
Adam Pierce
Seconded - please accept the answer you chose.
Jonathan Leffler
A: 

Look up the FILE structure. You'll be wanting to use FILE pointers for this solution.

Paul Nathan
No - you won't be wanting to manipulate the innards of the FILE structure. Treat FILE pointers as an opaque type; use them with the standard I/O library functions, or functions built atop those. Do not poke around the insides of a FILE unless you are really sure you know what you are doing.
Jonathan Leffler