KjellKod
10th August 2011, 20:56
By a mere chance I put in £ characters in a QString when I was unit testing write to/from text file.
I noticed that using QTextStream to read back the data lost £ characters.
Is it a locale thing? I've tried in on ubuntu only, not on Windows. Here's an example below where I show writing the text and then reading it back again. file.readAll() works like a charm but the other one doesn't
#include <iostream>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QIODevice>
namespace
{
const QString funky_text = " Yalla three:£[£££] \\£ three:#[###] \\# three:@[@@@] \\@ three:$[$$$] \\$ "; // with whitespace
void writeFile()
{
QFile file_out("/tmp/dummy.txt");
const QIODevice::OpenMode write_mode = (QIODevice::WriteOnly | QIODevice::Truncate);
if(!file_out.open(write_mode))
{
std::cerr << "Open (W) ERROR what's up:" << file_out.errorString().toStdString().c_str()<< std::endl << std::flush;
return;
}
QTextStream stream(&file_out);
stream << funky_text;
file_out.close();
}
void readVerify_1()
{
QFile file_in("/tmp/dummy.txt");
const QIODevice::OpenMode read_mode = (QIODevice::ReadOnly | QIODevice::Text);
if(!file_in.open(read_mode))
{
std::cerr << "Open (R) ERROR what's up:" << file_in.errorString().toStdString().c_str() << std::endl << std::flush;
return;
}
QString txt_in_1(file_in.readAll()); // conversion from QByteArray
file_in.close();
std::cout << "funky:" << funky_text.toStdString().c_str() << std::endl;
std::cout << "QString(funky, txt1) = " << QString::compare(txt_in_1, funky_text, Qt::CaseSensitive) << std::endl;
std::cout << "txt1: " << txt_in_1.toStdString().c_str() << std::endl;
}
void readVerify_2()
{
// read it again but by using QTextStream
QFile file_in("/tmp/dummy.txt");
const QIODevice::OpenMode read_mode = (QIODevice::ReadOnly | QIODevice::Text);
if(!file_in.open(read_mode))
{
std::cerr << "Open (R) ERROR what's up:" << file_in.errorString().toStdString().c_str() << std::endl << std::flush;
return;
}
QTextStream stream(&file_in);
QString txt_in_2 = stream.readAll();
file_in.close();
std::cout << "QString(funky, txt2) = " << QString::compare(txt_in_2, funky_text, Qt::CaseSensitive) << std::endl;
std::cout << "txt2:" << txt_in_2.toStdString().c_str() << std::endl;
}
void testQTextStream()
{
writeFile();
readVerify_1();
readVerify_2();
}
} // namespace
This would give the following output
funky: Yalla three:£[£££] \£ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
QString(funky, txt1) = 0
txt1: Yalla three:£[£££] \£ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
QString(funky, txt2) = -103
txt2: Yalla three:[] \ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
txt2: Yalla three:[] \ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
Obviously the £ signs are gone!
Anyone with ideas?
-- KjellKod
I noticed that using QTextStream to read back the data lost £ characters.
Is it a locale thing? I've tried in on ubuntu only, not on Windows. Here's an example below where I show writing the text and then reading it back again. file.readAll() works like a charm but the other one doesn't
#include <iostream>
#include <QFile>
#include <QTextStream>
#include <QString>
#include <QIODevice>
namespace
{
const QString funky_text = " Yalla three:£[£££] \\£ three:#[###] \\# three:@[@@@] \\@ three:$[$$$] \\$ "; // with whitespace
void writeFile()
{
QFile file_out("/tmp/dummy.txt");
const QIODevice::OpenMode write_mode = (QIODevice::WriteOnly | QIODevice::Truncate);
if(!file_out.open(write_mode))
{
std::cerr << "Open (W) ERROR what's up:" << file_out.errorString().toStdString().c_str()<< std::endl << std::flush;
return;
}
QTextStream stream(&file_out);
stream << funky_text;
file_out.close();
}
void readVerify_1()
{
QFile file_in("/tmp/dummy.txt");
const QIODevice::OpenMode read_mode = (QIODevice::ReadOnly | QIODevice::Text);
if(!file_in.open(read_mode))
{
std::cerr << "Open (R) ERROR what's up:" << file_in.errorString().toStdString().c_str() << std::endl << std::flush;
return;
}
QString txt_in_1(file_in.readAll()); // conversion from QByteArray
file_in.close();
std::cout << "funky:" << funky_text.toStdString().c_str() << std::endl;
std::cout << "QString(funky, txt1) = " << QString::compare(txt_in_1, funky_text, Qt::CaseSensitive) << std::endl;
std::cout << "txt1: " << txt_in_1.toStdString().c_str() << std::endl;
}
void readVerify_2()
{
// read it again but by using QTextStream
QFile file_in("/tmp/dummy.txt");
const QIODevice::OpenMode read_mode = (QIODevice::ReadOnly | QIODevice::Text);
if(!file_in.open(read_mode))
{
std::cerr << "Open (R) ERROR what's up:" << file_in.errorString().toStdString().c_str() << std::endl << std::flush;
return;
}
QTextStream stream(&file_in);
QString txt_in_2 = stream.readAll();
file_in.close();
std::cout << "QString(funky, txt2) = " << QString::compare(txt_in_2, funky_text, Qt::CaseSensitive) << std::endl;
std::cout << "txt2:" << txt_in_2.toStdString().c_str() << std::endl;
}
void testQTextStream()
{
writeFile();
readVerify_1();
readVerify_2();
}
} // namespace
This would give the following output
funky: Yalla three:£[£££] \£ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
QString(funky, txt1) = 0
txt1: Yalla three:£[£££] \£ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
QString(funky, txt2) = -103
txt2: Yalla three:[] \ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
txt2: Yalla three:[] \ three:#[###] \# three:@[@@@] \@ three:$[$$$] \$
Obviously the £ signs are gone!
Anyone with ideas?
-- KjellKod