Thread: Print string with escape characters

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58

    Print string with escape characters

    Hello there,

    This may be a silly question, but is there any function to print a string with all the escape characters in it?

    For example, instead of printing:
    This is a string.

    It would print:
    This is a string.\n
    Or in Windows:
    This is a string.\r\n

    I know that you can debug the code to find the variable contents, but is there any way to accomplish this with some standard library function?

    Thanks in advance.
    Last edited by koplersky; 03-07-2013 at 07:31 PM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I'm not aware of any such standard library functions ... I think escape sequences are handled in the translation phase, which I believe happens somewhere in preprocessing.

    You could write a function to handle this printing to the screen yourself based on the character value, but I'm guessing you already knew that.

  3. #3
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Im not sure why you are asking for this, but if you only need to be able to see them, many text editors can display hidden characters.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    A way of implementing this yourself would be to read in each character one by one from the file. When you come across an escape character, analyze it and print out the escape character as you want.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    I was thinking about doing this on my own but I thought there was some standard library function to do that.
    Thank you guys for the suggestions and for the help!

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    505
    Quote Originally Posted by koplersky View Post
    I was thinking about doing this on my own but I thought there was some standard library function to do that.
    Thank you guys for the suggestions and for the help!
    You have to roll your own.

    The function char *strtoescaped(char *str) shouldn't be hard to write. Just replace all the special characters with '/' plus the code, passing through for a first time to work out how much space to allocate.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Brazil
    Posts
    58
    Ok, thank you Malcolm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A way to print the control characters?
    By gratiafide in forum C Programming
    Replies: 3
    Last Post: 12-05-2012, 03:50 AM
  2. Escape Characters
    By GokhanK in forum C Programming
    Replies: 7
    Last Post: 02-24-2011, 08:01 PM
  3. stringstream with escape characters
    By Queue in forum C++ Programming
    Replies: 1
    Last Post: 11-22-2006, 06:24 PM
  4. how to print characters of own language?
    By kantze in forum C Programming
    Replies: 4
    Last Post: 10-17-2006, 12:22 PM
  5. Escape characters
    By The Gweech in forum C++ Programming
    Replies: 3
    Last Post: 07-09-2002, 05:38 PM