简体   繁体   中英

How can I get input via file without opening and closing the file in C?

I want to get input from a file in C without opening and closing it, like with use of fscanf or fgets . I just want to redirect my stdin to a file.

You can always use redirection from the command line:

yourprogram < input_file

Use scanf as you normally would for keyboard input. Redirection is nice because you can get input from a file or the keyboard with the same program (if you don't use redirection, input will come from the keyboard).

Also, you can redirect output to a file:

yourprogram > output_file

Or both:

yourprogram < input_file > output_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM