简体   繁体   中英

Can system() echo the data from an input file to CMD?

I'm trying to save useless printf() functions, and echo the data that's in my input file (.txt) whenever I start the program, I'm trying to do it with a system() function that uses command redirections. https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true

The data that I'm trying to echo is the intro and a set of rules:

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.

Is it possible to do so? if so, which redirect command should I use? can it be something like

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

?
Thanks.
EDIT:

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.

First of all, using system() instead of printf() just for avoiding printf() is a bad idea. If you really want to read from file, read this .

If this is why you don't want to 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.

You might want to check this out. It is possible to printf like this, all at once, without making the line of code utterly long.

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");

If you still insist on system() , you can do it like this:

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

With this, I wish you good luck on error handling (like file not found, wrong current directory).

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