3

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.

Ma250
  • 355
  • 1
  • 4
  • 11
  • 1
    Ick - why not just put that text into a single string constant and make one printf call to display it ? – Paul R Jan 06 '17 at 12:00
  • In Windows console you can print your instructions and launch your program from a batch file with two lines, first line: `@type input.txt` second line `@myprogram`. I am sure other systems have an equivalent. – Weather Vane Jan 06 '17 at 12:13

1 Answers1

1

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).

Community
  • 1
  • 1
raymai97
  • 806
  • 7
  • 19