Unwanted and annoying backslash in string read from file!
-
I want to read a unicode string from a file. file content is this:
\u0048\u0065\u006c\u006c\u006f
For reasons in my program, I have to use system calls to read the file. So I've done it through
QProcess
:execute->start("cat /home/user/x"); execute->waitForFinished(1000); qDebug()<<execute->readAllStandardOutput();
And the output is: "\\u0048\\u0065\\u006c\\u006c\\u006f". And it can't be decoded to "Hello" .
But when I enter it manually, it's fine!qDebug()<<"\u0048\u0065\u006c\u006c\u006f";
The output is: "Hello".
How should I overcome this problem?
Thanks in advance
-
@Mohammadsm said in Unwanted and annoying backslash in string read from file!:
For reasons in my program, I have to use system calls to read the file. So I've done it through QProcess:
FWIW this makes no sense at all. Running a
cat
process and reading all standard output in order to read a file is just a crazy inefficient equivalent of usingQFile
to read its content. I cannot imagine any circumstance where you would need to do this.... But I'll leave it at that.I want to read a unicode string from a file. file content is this:
\u0048\u0065\u006c\u006c\u006f
How did you get see that string? Do you mean the file length is 5 bytes or do you mean it actually has the string you show (~30 bytes) in it?
Next possibility:
qDebug()<<execute->readAllStandardOutput();
readAllStandardOutput()
returns aQByteArray
forqDebug()
to output.qDebug()<<"\u0048\u0065\u006c\u006c\u006f";
That is a string/
char[]
/QString
forqDebug()
to print.qDebug()
may well display aQByteArray
as an array of bytes but a string as a string, hence only the second one will be shown as a string"Hello"
. Don't rely onqDebug()
as an informative way to display content, it's intended to be human-readable only. -
Thank you @JonB
Do you mean the file length is 5 bytes or do you mean it actually has the string you show (~30 bytes) in it?
It's 30 bytes, just like it is.
\\u0048\\u0065\\u006c\\u006c\\u006f
I just want to know why there is an extra backslash in
QProcess::readAllStandardOutput()
?Don't rely on qDebug() as an informative way to display content, it's intended to be human-readable only.
I tried with
QTextCodec::fromUnicode(const QString &str)
but it failed due to this additional backslash. -
@Mohammadsm said in Unwanted and annoying backslash in string read from file!:
I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?
I guess because you used qDebug to print it...
-
@Mohammadsm said in Unwanted and annoying backslash in string read from file!:
It's 30 bytes, just like it is.
Then is not a Unicode string. And your "when I enter it manually as
qDebug()<<"\u0048\u0065\u006c\u006c\u006f";
" is totally different. Rather the file content is a "human visual representation of the 5 Unicode characters forming the C++ literal string". I do not know of any C++ or Qt function which would convert between the two, you would have to write one yourself. And just to complicate things further, your original was\u0048\u0065\u006c\u006c\u006f
, which is 30 bytes, but now you say the file contains\\u0048\\u0065\\u006c\\u006c\\u006f
which is 35 bytes and different again....I just want to know why there is an extra backslash in QProcess::readAllStandardOutput()?
readStandardOutput()
does not return/put in any "extra backslashes" (or any other character). I already informed you about the output format ofqDebug()
, which does not show precisely, one-for-one what characters you are looking at.I tried with QTextCodec::fromUnicode(const QString &str) but it failed due to this additional backslash.
First you have two posts claiming two different contents for the file, backslash-wise. Second if you really want to get rid of every backslash, or reduce double-backslashes to single-backslash, use something like
replace()
. And thirdly as I said above if your file does indeed contain either 30 or 35 bytes it is not a "Unicode string" (like your C++ string is) andQTextCodec::fromUnicode()
is not going to work on it or produce what you think.