-1

Why terminal command cat -v prints this text

$$$$
$$
$$$$$


¥
是我
§«Õϖ‡€
$$$$
$

$$
$$
$

like this?

$$$$
$$
$$$$$


Â¥
æM-^X¯æM-^HM-^Q
§«ÃM-^UÏM-^VâM-^@¡âM-^B¬
$$$$
$

$$
$$
$

I have a task to implement cat command in C. I've faced with this strange situation.

  1. Why this is so strange output?
  2. How to implement this behaviour in my program?

I'm working on Mac.

273K
  • 29,503
  • 10
  • 41
  • 64
bleschunov
  • 41
  • 6
  • Encoding issues? What is the encoding of the file? What are the settings for your terminal? – Some programmer dude Aug 26 '23 at 19:16
  • Also, the `cat` command is *very* simple. It basically doesn't do any processing of the input at all. It just reads the raw bytes from the input, and outputs the raw bytes without any kind of translation. – Some programmer dude Aug 26 '23 at 19:17
  • I can't replicate this. What is the value of `LANG` where you run the command? – jhnc Aug 26 '23 at 19:22
  • " -v Display non-printing characters so they are visible. Control characters print as ‘^X’ for control-X; the delete character (octal 0177) prints as ‘^?’. Non-ASCII characters (with the high bit set) are printed as ‘M-’ (for meta) followed by the character for the low 7 bits." (that's what `-v` does) – Diego Torres Milano Aug 26 '23 at 19:53
  • @Someprogrammerdude: Re “It basically doesn't do any processing of the input at all”: That is not correct. With `-v`, as stated in the question, `cat` replaces non-printing characters with visible representations. It also has options to display tabs, to remove multiple adjacent empty lines, to number the output lines, and to mark the ends of lines with `$`. (This is for Mac; other versions may vary.) – Eric Postpischil Aug 26 '23 at 20:12
  • Complementing the previous comments: If your file is, for instance, UTF8 or UTF16, but this is not reflected properly in the LANG variable, `cat` can not even properly recognize where a character starts and where it ends, let alone determine what a printable respectively a non-printable character would be. – user1934428 Aug 28 '23 at 06:26

1 Answers1

0

Typing man cat in the command line on your Mac should show the manual page for cat, including this information about the -v switch:

     -v      Display non-printing characters so they are visible.  Control
         characters print as `^X' for control-X; the delete character
         (octal 0177) prints as `^?'.  Non-ASCII characters (with the high
         bit set) are printed as `M-' (for meta) followed by the character
         for the low 7 bits.

How to implement this behaviour in my program?

Use isprint to determine whether a character is printable or not. If it is not, print output as described above.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312