Home

Raw and Cooked String Processing in C++

|
Updated:  
2016-03-26 08:29:42
|
From The Book:  
C++ Essentials For Dummies
Explore Book
Buy On Amazon

There are many ways to define literals. Of course, the kind of information that a literal affects is the most common method. However, literals can also be raw or cooked. A raw literal receives input from the application source and doesn’t interpret it in any way. What this means is that the information is interpreted character by character, precisely as the sender has presented it.

Cooked literals interpret the sender’s input and automatically perform any required conversions to make the data usable to the recipient.

The easiest way to see this principle in action is through an example. The RawAndCooked example shown demonstrates the technique used to create either raw or cooked string processing.

#include <iostream>
using namespace std;
int main()
{
    auto Cooked = "(HellornThere)";
    auto Raw = R"(HellornThere)";
    cout << Cooked << endl;
    cout << Raw << endl;
}

Most of the time when you see the rn combination, you know that the application will output a carriage return and line feed combination. This is the cooked method of processing a string.

The string is interpreted and any escape characters converted into control characters (characters that are normally regarded as commands, rather than data, such as the carriage return). However, notice how the Raw string is created. The R in front of the string tells the compiler to create the variable without interpreting the content.

Here’s the output you see from this example:

 (Hello
There)
HellornThere

Notice that the cooked form does output the parenthesis, but the raw form doesn’t. The parenthesis is required as part of the raw form input. As you might imagine, the cooked form outputs the rn combination as control characters, while the raw form outputs the actual characters.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Jeff Cogswell has been an application developer and trainer for 18 years, working with clients from startups to Fortune 500 companies. He has developed courses on C++ and other technologies.