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?

wasabi123
  • 173
  • 1
  • 13
  • 3
    then escape `\ ` with an extra `\ ` as `string unknown1="\\n\\t\\n\\t\\n\\n\\n\\n\\n";` – Grijesh Chauhan Apr 29 '15 at 03:38
  • The problem is i don't know what is inside unknown1.... it is read from a file ..... – wasabi123 Apr 29 '15 at 03: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)"` – chris Apr 29 '15 at 03:50
  • @chris `std::string` does not have a function dedicated to finding/replacing characters. – brettwhiteman Apr 29 '15 at 04:14
  • @Brett, I was referring to [this](http://en.cppreference.com/w/cpp/string/basic_string/replace) 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`. – chris Apr 29 '15 at 04:16
  • 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 Apr 29 '15 at 04:35

4 Answers4

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.

Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • The problem is i don't know what is inside unknown1.... it is read from a file .... – wasabi123 Apr 29 '15 at 03:47
  • @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 `\ ` – Grijesh Chauhan Apr 29 '15 at 03:49
  • 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 Apr 29 '15 at 04:35
  • @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 ). – Arun A S Apr 29 '15 at 04:44
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

  • no issue conceptually you are also correct, so it is an helpful answer to OP – Grijesh Chauhan Apr 29 '15 at 03:55
  • [do not use an EOF check in a loop condition](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – M.M Apr 29 '15 at 04:04
  • @Matt McNabb I had never seen this before(I only learned C three months ago). Thanks for the tip! – FreeSandwiches Apr 29 '15 at 04:08
  • 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 Apr 29 '15 at 04:36
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;
}
brettwhiteman
  • 4,210
  • 2
  • 29
  • 38
  • 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 Apr 29 '15 at 04:37
  • @wasabi123 consult the compiler documentation or the C++ standard to find a list of all possible escape characters. – brettwhiteman May 19 '15 at 04:44
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 );
Carl
  • 993
  • 8
  • 14
  • 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 Apr 29 '15 at 04:36
  • 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. – Carl Apr 29 '15 at 10:41