2

hi i have a unknown string in c++ containing "\n" "\t" etc. say;

string unknown1=read_from_file();

if unknown1="\n\t\n\t\n\n\n\n\n" now I want to print

"\n\t\n\t\n\n\n\n\n"

to the screen explcitly other than a bunch of empty spaces.... how should I do that? remember that I don't know what is in unknown1...

to emphasize, I know that we could print \n explicitly if we change "\n" into "\n" for every such character... But the problem is that I don't know what is inside unknown1... It is read from a file....

Thanks for answering, however we have further concerns:

The procedure answered has one more porblem... Suppose I don't know that the only possible special character in the file is \n or \t... maybe there are something like \u ? \l ? i think we can't exhaust every possibility right? Is there kind of built in C++ function just to output the corresponding characters?

6
  • 3
    then escape \ with an extra \ as string unknown1="\\n\\t\\n\\t\\n\\n\\n\\n\\n"; Commented Apr 29, 2015 at 3:38
  • The problem is i don't know what is inside unknown1.... it is read from a file ..... Commented Apr 29, 2015 at 3:47
  • Okay, so replace (I mean std::string has a function dedicated to that) newline characters with the string you want and tab characters with the other string you want. By the way, for literals, no need for escaping the crap out of everything: R"(\n\t\n\t\n\n\n\n\n)" Commented Apr 29, 2015 at 3:50
  • @chris std::string does not have a function dedicated to finding/replacing characters. Commented Apr 29, 2015 at 4:14
  • @Brett, I was referring to this as a dedicated function for replacing a character with a string (I didn't say finding, but there's one for that, too). I'm aware you have to call it multiple times in order for every occurrence to be replaced, unlike replacing single characters with single characters, which is one call to std::replace. Commented Apr 29, 2015 at 4:16

4 Answers 4

4

\n , \t are escape sequences, but you can print them by adding an extra \ before them, \\ is used to obtain a single backslash. A single backslash means it is an escape sequence ( if it is a valid one ), but two backslashes represent the backslash character, so whenever you need to output a backslash, just add two of them.

So, if you use

string unknown1="\\n\\t\\n\\t\\n\\n\\n\\n\\n";

You will get your desired output.

If you are reading from a file , then do something like this

string unknown1="\n\t\n\t\n\n\n\n\n";
for ( int i = 0 ; i < unknown1.length() ; i++ )
{
    if( unknown1[i] == '\n')
      cout<<"\\n";
}

Like that, you will have to check for each escape sequence that may be used.

Sign up to request clarification or add additional context in comments.

4 Comments

The problem is i don't know what is inside unknown1.... it is read from a file ....
@Arun OP wants to print file content as raw means if their is a new line in file then print \n rather than printing new line, it need to read file and scan byte by byte it their is any \ then write two \
The procedure indicated has one more porblem... Suppose I don't know that the only possible special character in the file is \n or \t... maybe there are something like \u ? \l ? i think we can't exhaust every possibility right? Is there kind of built in C++ function just to output the corresponding characters?
@wasabi123 , it does have that problem, but as far as I know, there is no function for this ( but maybe you could just create one for further use ). If there was, then we wouldn't have posted answer's like this. The only thing you can do right now is hard code it ( I had provided a link to all the escape sequences in my answer just for this ).
1

Run specific checks for the non-printable characters you are worried about like this.

char c;
while(c!=EOF){
    if(c=='\n')printf("\\n");
    if(c=='\t')printf("\\t");

    and so on and so forth.
    c = the next charater;
}

oops, I wrote C instead of C++ but @Arun A.S has the right syntax

4 Comments

no issue conceptually you are also correct, so it is an helpful answer to OP
@Matt McNabb I had never seen this before(I only learned C three months ago). Thanks for the tip!
The procedure indicated has one more porblem... Suppose I don't know that the only possible special character in the file is \n or \t... maybe there are something like \u ? \l ? i think we can't exhaust every possibility right? Is there kind of built in C++ function just to output the corresponding characters?
1

See below example. You can add your own characters to the switch to extend the characters it handles.

#include <iostream>
#include <string>

std::string escapeSpecialChars(const std::string& str)
{
    std::string result;

    for(auto c : str)
    {
        switch(c)
        {
            case '\n':
                result += "\\n";
                break;

            case '\t':
                result += "\\t";
                break;

            default:
                result += c;
                break;
        }
    }

    return result;
}

int main()
{
    std::string str = "\n\n\n\t";

    std::cout << escapeSpecialChars(str);

    return 0;
}

2 Comments

The procedure indicated has one more porblem... Suppose I don't know that the only possible special character in the file is \n or \t... maybe there are something like \u ? \l ? i think we can't exhaust every possibility right? Is there kind of built in C++ function just to output the corresponding characters?
@wasabi123 consult the compiler documentation or the C++ standard to find a list of all possible escape characters.
0

You could create your own function to print characters using a std::map:

void printChar( const char ch )
{
  static const std::map< char, std::string > char_map = {
    { '\n', "\\n" }
    // add mappings as needed
  };

  auto found = char_map.find( ch );
  if ( found != char_map.end() )
    std::cout << found->second;
  else
    std::cout << ch;
}

// usage
std::string str = "a\nbc";
for ( auto ch : str ) printChar ( ch );

2 Comments

The procedure indicated has one more porblem... Suppose I don't know that the only possible special character in the file is \n or \t... maybe there are something like \u ? \l ? i think we can't exhaust every possibility right? Is there kind of built in C++ function just to output the corresponding characters?
That is why I added the comment to add character mappings as needed. If you need tab and/or other special characters, just put the time into adding them to the map.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.