Convert string with explicit escape sequence into relative character

前端 未结 5 1536
离开以前
离开以前 2020-12-06 17:13

I need a function to convert \"explicit\" escape sequences into the relative non-printable character. Es:

char str[] = \"\\\\n\";
cout << \"Line1\" <         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-06 18:08

    Here's a cute way to do it on Unixy platforms.

    It calls the operating system's echo command to make the conversion.

    string convert_escapes( string input )
       {
       string buffer(input.size()+1,0);
       string cmd = "/usr/bin/env echo -ne \""+input+"\"";
       FILE * f = popen(cmd.c_str(),"r"); assert(f);
       buffer.resize(fread(&buffer[0],1,buffer.size()-1,f));
       fclose(f);
       return buffer;
       }
    

提交回复
热议问题