PDA

View Full Version : Reading a .pro file using custom read function



Ahmed Abdellatif
7th October 2017, 10:39
I am trying to read the .pro file of my project and display it on the console window. The reading was done successfully but the displaying was not as expected. The program displays the text as sequential letters:


QT += core\nQT -= gui\nCONFIG += c++11\nTARGET = 13-ResourceFiles\nCONFIG += console\nCONFIG -= app_bundle\nTEMPLATE = app\n\nSOURCES += main.cpp\n\n

not as :

QT += core
QT -= gui


CONFIG += c++11

TARGET = 13-ResourceFiles
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app

SOURCES += main.cpp

Here is the Reading function:

void Read(QString file_name)
{
QFile myfile(file_name);
if(myfile.exists())
{
myfile.open(QIODevice::ReadOnly);
QString my_buffer;
my_buffer=myfile.readAll();
qDebug()<<my_buffer;
myfile.flush();
myfile.close();
}

high_flyer
7th October 2017, 11:23
Try this:



void Read(QString file_name)
{
QFile myfile(file_name);
if(myfile.exists())
{
if(myfile.open(QIODevice::ReadOnly)){
QTextStream stream(&myfile);
QString my_buffer = stream.realAll();
qDebug()<<my_buffer;
myfile.close();
}
}
}


Using readAll() is not recommended unless you can assert that the size of the read files is not very large.

Ahmed Abdellatif
7th October 2017, 11:31
I can not see any difference in the result. what did you modify?

Lesiok
7th October 2017, 11:50
QDebug escapes non-printable characters. Try to change qDebug() with qDebug().noquote().