0

As you know we can use something like this:

 string s = L("some\nstr\t");

My question is if there is a way to print a string using a literal. For example something like this:

string s = "s\nsome\n"
cout<< L(s); // the output printed should be s\nsome\n and not new lines

Thank you.

Adrian
  • 947
  • 1
  • 12
  • 24
  • 2
    Write a function that escapes backslashes and puts `'\"'` at the beginning and end of a string. – jdc Jun 11 '18 at 15:18
  • What do you mean by "_print a string as a literal_"? Please [edit] your post to clarify. – Ron Jun 11 '18 at 15:23
  • @hyde Related, but not duplicate because the link asks only about non-printable characters, which are quite easy to detect. – Holt Jun 11 '18 at 15:27
  • What you are looking for is not called "print string as literal". It is called "escaping a string". There are various escape encodings. C and C++ string literals use one. Another example would be URL encoding, yet another is Unish sh shell escape system (similar but not identical to C). – hyde Jun 11 '18 at 15:27
  • @Holt I think the duplicate I suggest provides answer to this question too. The question is rather unclear in fact... One answer would be "impossible", because string literal is not stored in binary as it is written in source code, so there is no way to recover the exact escape codes used in the source. – hyde Jun 11 '18 at 15:30
  • The question indeed is asked in very obscure manner, but the duplicate covers the real question :) – SergeyA Jun 11 '18 at 15:35

2 Answers2

1

Yes,

string s = "Here is \\nan \\n example"
cout<< s;
  • 1
    I think the assumption is that the OP needs conversion on a string they can't modify, i.e. they need `'\n'` converted to `"\\n"`, along with other escaped characters. – Xirema Jun 11 '18 at 15:20
  • Don't understand down votes. –  Jun 11 '18 at 15:26
  • This is ok for short string. I was wondering if is not a way to this for large strings(for files >1GB of text) – Adrian Jun 11 '18 at 16:58
  • @John may be you need to refer to https://stackoverflow.com/questions/2417588/escaping-a-c-string which is dup of yours. –  Jun 12 '18 at 07:19
0

Francois' answer is already good, but I'd like to elaborate a little more about what's happening here...

When you put \n in a string, that is a single character. The \ is saying "don't treat whatever comes next as you normally would, escape it." An escaped n is a newline, so \n is the newline character.

So if you want \ in your string, how would you get it? Normally this is treated as the escape character, but we want it to act as just a regular character. So how do we do that? We escape it! \\ will be interpreted as a single \ character.

So if you want s\nsome\n to be printed, you need to construct your string as "s\\nsome\\n". Notice that the n's aren't being escaped, the escape characters are!

scohe001
  • 15,110
  • 2
  • 31
  • 51
  • This is the easy way. In order to this, I should parse my entire string. For example, you read a file of 1GB I don't know if is a good idea to parse the entire file and to add escape character. – Adrian Jun 11 '18 at 16:57
  • @John ahh in that case, yes. You'd do a find and replace looking for character `'\n'` (newline) and replacing with the two characters `"\\n"` (escaped escape and n). You'd also do the same for tab/carriage return/whatever other escapes you wanted to add. – scohe001 Jun 11 '18 at 17:05
  • Ok, I was thinking maybe there is a lite way to do this without creating more overload. I will try your suggestions. – Adrian Jun 11 '18 at 17:10
  • @John also take a look at the marked duplicate. I believe it's doing exactly what you're asking. – scohe001 Jun 11 '18 at 17:11