gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:29:14 25 4
gpt4 key购买 nike

我正在尝试保存无用的 printf() 函数,并在每次启动程序时回显输入文件 (.txt) 中的数据,我正在尝试使用使用命令的 system() 函数来实现重定向。 https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

我要回应的数据是介绍和一组规则:

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.

The rules are as follows:
1. In each round you try to guess the secret password (4 distinct digits)
2. After every guess you'll receive two hints about the password HITS: The number of digits in your guess which were exactly right.
MISSES: The number of digits in your guess which belongs to the password but were miss-placed.

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

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

?
谢谢。
编辑:

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

最佳答案

首先,使用 system() 而不是 printf() 只是为了避免 printf() 是一个坏主意。如果你真的想从文件中读取,read this .

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

I really want to avoid using a single printf() and the whole text in it and using "\n" etc. or using lots of printf() functions (as stated above) for each sentence etc.

你可能想看看这个。可以像这样一次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");

有了这个,我祝你在错误处理方面好运(比如找不到文件,当前目录错误)。

关于system() 能否将输入文件中的数据回显到 CMD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41505212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com