Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
system()能否将输入文件中的数据回显到CMD?_C - Fatal编程技术网

system()能否将输入文件中的数据回显到CMD?

system()能否将输入文件中的数据回显到CMD?,c,C,我试图保存无用的printf()函数,并在每次启动程序时回显输入文件(.txt)中的数据,我试图使用使用命令重定向的system()函数来完成。 我试图回应的数据是简介和一组规则: 选择了一个秘密密码来保护Pancratius的信用卡, 安条克的后裔。 你的任务是通过泄露潘克拉提乌斯的秘密密码来阻止他 规则如下: 1.在每一轮中,您都会尝试猜测密码(4个不同的数字) 2.每次猜测后,您都会收到关于密码点击的两条提示:猜测中准确的位数。 未命中:您的猜测中属于的位数 密码已更改,但未正确放置 有

我试图保存无用的printf()函数,并在每次启动程序时回显输入文件(.txt)中的数据,我试图使用使用命令重定向的system()函数来完成。

我试图回应的数据是简介和一组规则:

选择了一个秘密密码来保护Pancratius的信用卡, 安条克的后裔。 你的任务是通过泄露潘克拉提乌斯的秘密密码来阻止他

规则如下:
1.在每一轮中,您都会尝试猜测密码(4个不同的数字)
2.每次猜测后,您都会收到关于密码点击的两条提示:猜测中准确的位数。
未命中:您的猜测中属于的位数 密码已更改,但未正确放置

有可能吗? 如果是,我应该使用哪个重定向命令? 会是这样吗

system("program.exe < input.txt")
system(“program.exe

谢谢。
编辑:


我真的想避免使用一个printf()和其中的整个文本,并使用“\n”等,或者对每个句子等使用大量printf()函数(如上所述)。

首先,使用
system()
而不是
printf()
仅仅为了避免
printf()
是个坏主意。如果确实要从文件中读取

如果这就是您不想打印的原因:

我真的希望避免使用单个printf()和其中的整个文本,并使用“\n”等,或为每个句子等使用大量printf()函数(如上所述)

你可能想看看这个。可以像这样同时
printf
,而无需使代码行非常长

printf(
    "A secret password was chosen to protect the credit card of Pancratius, "
    "the descendant of Antiochus. Your mission is to stop Pancratius by "
    "revealing his secret password. \n"
    "The rules are as follows: \n"
    "1. In each round you try to guess the secret password (4 distinct digits) \n"
    "2. After every guess you'll receive two hints about the password \n"
    "HITS: The number of digits in your guess which were exactly right. \n"
    "MISSES: The number of digits in your guess which belongs to the "
    "password but were miss-placed.\n");
如果您仍然坚持使用
system()
,您可以这样做:

system("cmd.exe /c type yourInput.txt");

有鉴于此,我祝愿您在错误处理方面好运(如未找到文件、错误的当前目录)。

单击-为什么不将该文本放入单个字符串常量中,并进行一次printf调用以显示它?在Windows控制台中,您可以打印指令并从带有两行的批处理文件启动程序,第一行:
@type input.txt
第二行
@myprogram
。我相信其他系统也有类似的功能。