简体   繁体   English

如何在不打开和关闭C文件的情况下通过文件获取输入?

[英]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 . 我想从C中的文件中获取输入而不打开和关闭它,就像使用fscanffgets I just want to redirect my stdin to a file. 我只是想将我的stdin重定向到一个文件。

You can always use redirection from the command line: 您始终可以从命令行使用重定向:

yourprogram < input_file

Use scanf as you normally would for keyboard input. 像往常一样使用scanf进行键盘输入。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有在C中的Linux上打开文件的情况下检查我是否有权打开文件? - How can I check if I have permissions to open a file without opening it on Linux in C? 如何判断在C中打开输入文件是否失败 - How to tell if opening input file failed in C 识别文件中的C函数(大括号) - Identify C functions (opening and closing braces) in a file 我可以在不关闭文件描述符的情况下打开多个popen吗? - can i open multiple popen without closing file-descriptor? 将字符写入文件并替换上一个字符,而无需每次都打开和关闭 - Write characters to file and replace the previous one without opening and closing everytime 在Unix上的C中,进程如何在不打开文件的情况下告诉文件它具有什么权限? - In C on Unix, how can a process tell what permissions it has to a file without opening it? 打开和关闭日志文件导致 C 中的 memory 泄漏 - Opening and closing a log file leads to memory leaks in C 在 C 中多次打开和关闭同一个文件是一种不好的做法吗? - Is opening and closing the same file multiple times a bad practice in C? 我如何在C中获取目录中的所有文件名而又不获取“。”和“ ..” - how can i get all file name in a directory without getting “.” and “..” in C 如何在不使用fseek或stat的情况下使用C获得文件的大小? - How can I get a file's size in C without using either fseek or stat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM