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

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

Related

UNIX redirections from files for both input and output - that also shows what was the input in the output file

Sorry if I phrased the question a bit weird.
I'm trying to debug a program, and I'd like to redirect both the input and output from-to files, like this:
MyProgram.out << MyInput.txt >> MyOutput.txt
My problem with this is that I also need the output file to contain the input of every function.
For example, let's say that MyInput.txt has one line with the number 3, and MyProgram.out only asks from the user to input a number.
So what I'll get is:
Choose a number:
But what I'd like to get is (written in MyOutput.txt):
Choose a number: 3
Is there a way to make it show the inputs as well?
What you are trying to achieve is not possible, in the general case, without modifying MyProgram.out, as I answered here.
You are trying to interleave 2 pipes (input & output) in a synchronized fashion, where the synchronization is defined by MyProgram.out.
But pipes are asynchronous: the data (e.g. the line containing 3) has already been read from the file into the pipe before MyProgram.out even started, and now MyProgram.out is the one who decides whether to first read from the input pipe, and how many lines/bytes, or write something like Choose a number: to the output pipe.
So only MyProgram.out knows if Choose a number: should be written before 3, or vice versa.
There are some unclean ways you could still achieve that:
You could write a program that uses ptrace(2) to see if MyProgram.out reads the input first or writes the prompt first.
You could assume that every line of output corresponds to every line of input - which isn't the case in your program because when it writes Choose a number: it doesn't add a \n.
However, script only looks at the terminal, and you redirect the output from a file, not the terminal, so using script won't help you here.

how to print with a while also using a scanf statement

I have an odd situation ive made a login system that has many printf and color statements
______________________________________________________________
example border_red(); printf (" $$ ");
_____________________________________________________________
I have multiple printf statements in the menu and in the middle of my code I have a scanf statment to scan the username. The menu gets cut off halfway through making just the top part of the menu appear.
WHAT THE MENU IS SUPPOST TO LOOK LIKE
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ username:type here $
$ $
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
WHAT THE MENU LOOKS LIKE
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$ username: type here
i need the entire menu to show when you type in your username
QUESTION: How would i go about printing letters after a scanf statement
char username[50];
printf("#############################");
pritnf("# username:");
scanf(log, & username);
printf(# #');
printf("#############################");
how would i go about not having the scanf statement cut off my menu
Sorry if this is a simple question I may have googled it wrong but ive been at this for 1 hour now and this is the only thing I can turn to.
Both scanf and printf assume line-oriented input and output (think of a hardcopy terminal or teletype). They're just not designed to print a border, then scan from a location within that border1.
If you want to set up borders or place input fields in arbitrary places on screen, you'll have to look beyond the standard C library and use a third-party tool like ncurses.
There are tricks you can use - you can send ANSI terminal escape sequences to place the cursor at arbitrary locations on the screen, but it's ugly and a lot of work. You're better off using a library like ncurses.

Convert string to decimal ascii (dec) representation in AHK

I've been struggling to get this to work for a little while and debugging it has been a pain... I'm running into a few different issues.
I prompt for user input something like this:
InputBox, stuff, Enter information, Enter obfuscated information in the box below,,,,,,,30000,
dexStuff= ""
Loop, Parse, stuff
dexStuff:= AscToDec(%A_LoopField%)
The AscToDec function is painfully simple:
AscToDec(c){
return Asc(c)
}
This is ending up with dexStuff as 0000 when I enter "test" as my string. If I change the call of AscToDec() to just MsgBox %A_LoopField% it prints out t e s t in different popups.
Can someone help me understand what I'm doing wrong here?
To concatenate the converted characters of your input, use this:
InputBox, stuff, Enter information, Enter obfuscated information in the box below,,,,,,,30000,
Loop, Parse, stuff
dexStuff := dexStuff . Asc(A_LoopField)
MsgBox %dexStuff%
I see two bugs in your code. First, initializing dexStuff isn't necessary, but if you do so, use either dexStuff= or dexStuff:="", not dexStuff="" which sets dexStuff to two quotation marks. Second, you don't need to dereference A_LoopField in your function call by using percent signs.
Here is your corrected code
InputBox, stuff, Enter information, Enter obfuscated information in the box below,,,,,,,30000,
dexStuff:="" ; := operator, not =
Loop, Parse, stuff
dexStuff:= AscToDec(A_LoopField) ; A_LoopField not %A_LoopField%

How to input of a string which contain '\n' in it

I have created an auto typing bot which simulate characters of string given by user.
My code is this:
printf("Enter speed (wpm) (1 to 11750):");
scanf("%d", &speed);
if(speed < 1 || speed > 11750)
{
printf("\nPlease provide CORRECT DATA\n");
return -1;
}
printf("Paste the String : \n");
gets(exaArray);
exaArrayLength = strlen(exaArray);
relation = (int)11750/speed;
printf("typing will start in 2 sec-\n");
Sleep(2000);
i=pos=0;
while(i<=exaArrayLength)
{
Sleep(relation);
if((exaArray[pos]>96) && (exaArray[pos]<123)) //small letters
{
keycode=0x41 + (exaArray[pos]%97);
smallLetter(keycode); //function for key simulation
}
.....
I am taking input using gets function. This program works fine when I paste text which does not contain Enter. So this program works fine with one paragraph.
But if the user provides more than one paragraph, then it simulates only the first paragraph.
Because gets terminates at '\n'. Which function could take multiple paragraph input and assign it to a string.
This is actually a very hard and complex problem, and not easy to solve in an automated way.
It would seem like reading in a loop would be a good solution, but then you come to the point when there is no more input and the reading function just blocks waiting for more input. The easiest way out of this is to have the user enter the "end of file" key combination (CTRL-D on POSIX systems like Linux or OSX, CTRL-Z on Windows), but the user must then be told to do that.
The problem stems from that your program simply have no idea how much data it is expected to read, and there is no function which is able to, basically, read the users mind when the user thinks "that's it, no more data".
Besides the above solution to have the user give an "end of file", you can use other sequences or special keys or even phrases of input to mark the end, but it all comes down to this: Read input in a loop until users says "no more input".
Well, there is no way for the computer to make a difference between the user pressing enter and the "pasted" string containing newlines. (Technically, pasting something into the console is like typing it.)
If you just don't want the problem to exit after one paragraph but continue, you can do it as commenter alk suggested (loop around the reading function) - then you would need Ctrl+C to exit the program and then there would technically still be one paragraph at a time written. Depends on what you further want to do with the program.
On the other hand, if you want a way for the user to input all text at once and only then process it, you would need to define something other than "newline" as "end of input" marker, for example something like ESC.
You would do this by using getchar instead of gets and manually concatenating each char which is entered this way to a string buffer, and if the character has the value 27 (escape key) for instance, you would end the input loop and start outputting.

How to write couple of lines into a file in C?

I'm having a variable called username and another one called password...
I need to register users into a system, and write it on a file..
My problem is, that the 2nd time or more, it writes the details on the same line..
For example :
in the first time I write for the file, it become something like this :
Amit 123456
but on the second time I'm registering a user, it doesnt go to the next line and keep writing from that spot:
Amit 123456Yosi 125818
where I need the file to be like this :
Amit 123456
Yosi 125818
I mean, I need to make the 2nd or more fprintf to the file go to the next line, and not to start from the specific place...
I've wrote this in the code :
fprintf(file, "%s %s\n", username, password);
though, the '\n'doesnt seems to make the next fprintf go to the next line...
In summery - I need to print to the file every time, on a new line..
on the first print to the file, it'll write on line 1
then on the second print to the file, it'll write on line 2, and not from the continue of line1...
Thanks :)
Had the same thought as the commenters: You may need a \r in there too, depending on your development environment. It could even be that the file viewer you are using is the issue (eg: Notepad.exe on Windows often has trouble with simple line endings)
I'm guessing you're on Windows, you need /r/n
Note that it is very, very bad practice to save passwords, please google how to work with passwords.

Resources