繁体   English   中英

使用 scanf() c 从标准输入读取文件名

[英]Reading the file names from standard input using scanf() c

如何制作一个程序,用户可以使用 scanf 在标准输入上键入他们想要读取和写入的文件名(输入和输出文件)? 例如,当他们运行程序 ./start filename1 filename2 (在 C 中)。

如果他们以这种方式运行程序:

./start filename1 filename2

输入和输出文件名被传递给argv数组中的main函数。 不需要也确实没有办法使用scanf来检索它们。

首先学习如何打开文本文件以及如何编写文本。

http://www.cplusplus.com/reference/cstdio/fopen/

如果你知道,你可以在主函数(argc,argv)中取参数,这会给你在命令行中输入的参数的信息

int main(int argc, char *argv[])
{
     //arc : how many value are in the command line
     //argv : array of string, string from each parameter in the command line
     //argc = 3
     //argv = {"./start", "filename1", "filename2"}
     return 0;
}

之后您可以使用 scanf 从用户那里获取价值。 在每次 scanf 之后,您可以写入文件。 最后不要忘记关闭文件。

暂无
暂无

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

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