3
string str = "hello/nblabla/nblabla";
cout << str << endl;

how do I make output of this program be: hello/nblabla/nblabla if I can't use // and generally don't know what string contains. I would like to do the same with c-style char strings as well, and I need to print /0 if it appears in the middle of a c-style string.

I have code something like:

fin >> str;
stuff_to_do(str);
cout << str << endl;

where fin is a std::ifstream to a file containing data like: 123/nb/t/0/1/"... I can't reformat this file.

5
  • 4
    First of all, and sorry to be a nitpicker, but it's \ , not /. (Big difference.) Commented Jul 23, 2018 at 15:57
  • 1
    Make a lookup table and use it to convert special characters into escape sequences before printing. Commented Jul 23, 2018 at 15:59
  • You'll need to write a function that moves along the string a character at a time, printing it a character at a time. If the character you're looking at is '\n', print it as the two characters '\' and 'n'. If it's any other special character, print it using some other special character sequence. Otherwise (if it's a regular character), print it as itself. Commented Jul 23, 2018 at 16:01
  • Does your input file contain the two characters '\' and 'n', or the single character '\n'? (Again, big difference.) Commented Jul 23, 2018 at 16:04
  • 2 characters \ and n Commented Jul 23, 2018 at 16:08

2 Answers 2

2

Use raw string literal.

string str = R"(hello\nblabla\nblabla)";
cout << str << endl;

output: hello\nblabla\nblabla
this works for null-terminated strings too!

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

Comments

1

I think you need an escaping function that takes arbitrary text and escapes it so that it uses the syntax of a C++ string literal (minus the quotes). Maybe look around the web to see if there's a library already doing this.

There are some conventions to apply because there are multiple ways to escape a character.

I don't love this code, but here's an implementation I quickly put together to illustrate:

#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>

const char* staticMap(char c) {
    switch (c) {
        case '\a': return "\\a";
        case '\b': return "\\b";
        case '\t': return "\\t";
        case '\n': return "\\n";
        case '\v': return "\\v";
        case '\f': return "\\f";
        case '\r': return "\\r";

        case '\"': return "\\\"";
        case '\'': return "\\\'";
        case '\?': return "\\\?";
        case '\\': return "\\\\";
    }
    return nullptr;
}

std::string escape_cpp(const std::string& input) {
    std::stringstream ss;
    for (char c : input) {
        const char* str = staticMap(c);
        if (str) { 
            ss << str;
        } else if (!isprint(static_cast<unsigned char>(c))) {
            ss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << (static_cast<unsigned int>(static_cast<unsigned char>(c)));
        } else {
            ss << c;
        }
    }
    return ss.str();
}

int main() {

    std::string foo("bu\u00ffffalo\n\"\tw\u0000rld\"", 17);
    // Will be printed as written (by convention).
    std::cout << escape_cpp(foo) << std::endl;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.