-1

So I've seen these two questions:

And unfortunately these did not provide me for an answer I've needed let's say I'm dependent within some output of a certain program and can't tell for sure which values I need to enter ahead of program's execution, I'd rather enter some input for example for this code:

#include <stdio.h>

int main()
{
    char a[10] = {0};
    printf("\n----------- %x\n", (unsigned int) a); 
    scanf("%s", a);
    for(int i = 0; i < 10;  i++)
        printf("%d\n", (int) a[i]);
}

as an example, I'm waiting after the printf within the following program has reached, then

input -> some input containing non-printable ascii characters

program output-> 04 08 01 05 02 10 09 12 02 02 (All these are values of non-printable ascii characters)

and again I'm looking for a way to enter them directly

  • I'm not looking for a way to do this by implementing some functionality within my code, my code is given as an example of a program
  • When saying during runtime, I mean after the program has started running - hence the prints within my program have reached
  • I apologize if this was unclaer
Community
  • 1
  • 1
DrPrItay
  • 793
  • 6
  • 20
  • 1
    One of the answers gives: `printf '\x08\xDE\xAD' | yourprogram`. That is entered during runtime as you require. Please clarify why it is not acceptable for you so that the right solution can be given. – kaylum Jan 16 '17 at 19:50
  • This isn't runtime as well, printf '\x088\xDE\xAD' runs first then entered into stdin as the input for my_program, my point is that for example my output is dependent of the prints within my_program hence it's not acceptable @kaylum – DrPrItay Jan 16 '17 at 19:54
  • The problem you will have is depending on the shell you are using. In a bash shell, you could use `echo -e "\x04"` to type in the ASCII code 4. You won't be able to just type in 4 or something similar. – bruceg Jan 16 '17 at 19:56
  • It's not that complicated, please read my question, I can't enter my input BEFORE THE PROGRAM RUNS I'm dependent of the prints within my program hence I must enter it during the runtime of the program (after the prints within my program were reached) – DrPrItay Jan 16 '17 at 19:57
  • 2
    @DrPrItay Then do what @MarkTolonen suggested in the answer below. `scanf` is not designed to work with binary data and escape characters. You have to implement this functionality yourself. – Eugene Sh. Jan 16 '17 at 19:59
  • 1
    when saying runtime I mean after the program has started executing, - after prints are reached for exampe, @kaylum – DrPrItay Jan 16 '17 at 20:00
  • @EugeneSh. I'm looking for a way of doing that without implementing my own functionallity, perhaps posting my code here is useless, as I'm not looking for a way to change my code, I just gave it as an example of some program which I would be required to do such a thing – DrPrItay Jan 16 '17 at 20:02
  • So it is not possible. Would that answer suffice? – Eugene Sh. Jan 16 '17 at 20:03
  • No as I know it is possible, (usage of ctrl + shift + u ) that unfortunately doesn't work fully for me @EugeneSh. – DrPrItay Jan 16 '17 at 20:06
  • @EugeneSh. look at Barmar's answer, it is possible – DrPrItay Jan 16 '17 at 20:13
  • What will you do if you want to enter `\0x00` or `\n`? – Eugene Sh. Jan 16 '17 at 20:14
  • https://en.wikipedia.org/wiki/Control_character @EugeneSh. I'd use barmar's method for control characters and ctrl + shift + u for other non-printable ascii values – DrPrItay Jan 16 '17 at 20:17
  • It doesn't matter how you enter it, by hitting "enter" or inputting some control character representing `\n` or a whitespace, it will eventually terminate your string reading, because it is how `scanf` works. To emphasize, what will be your input to get the code snippet to print the code for a whitespace (i.e. 0x20) ? – Eugene Sh. Jan 16 '17 at 20:21
  • again, try ctrl + shift + u with hex value or the control characters, worked for me @EugeneSh. – DrPrItay Jan 16 '17 at 20:24
  • You are telling me that you were able to get from the snippet above the output `"32 32 32"` by hitting `ctrl + shift + u 20` three times, for example? Unfortunately I can't test it myself, but I would believe you if you say so. – Eugene Sh. Jan 16 '17 at 20:27
  • Yup, exactly what I'm saying :) @EugeneSh. – DrPrItay Jan 16 '17 at 20:29
  • @DrPrItay `scanf("%s", a);` does not save space characters in `a`. [This is suspicious](http://stackoverflow.com/questions/41683969/input-non-printable-ascii-characters-into-scanf#comment70566096_41683969) that OP was able to get `"32 32 32"`. Request OP's clarification. – chux - Reinstate Monica Jan 16 '17 at 20:35
  • 1
    @chux This what is making me doubt. – Eugene Sh. Jan 16 '17 at 20:35

1 Answers1

2

On Linux you can type Control-v before a control character to enter it literally. So to type the character with code 0x04, type Control-vControl-d.

Barmar
  • 741,623
  • 53
  • 500
  • 612