You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Monday, November 7th 2011, 1:29pm

Qt qfile::readall()

Hi, i'd like to read a file with readall() function in qfile but seem return me always 0, so an "empty" qtstring. I've tryed another way using read(number of bytes) calculating size of document but even this method does not work.
Build is ok

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void MainWindow::open_file (){
	QString percorso_file; // file path
	percorso_file=QFileDialog::getOpenFileName(this,"Apri il file","","File di testo(*.txt .text);;File sviluppo(.c .h .c++ .src)");
	ui->lineEdit->setText (percorso_file); //show somewhere full path
	QFile file_aperto(percorso_file);
	QString contenuto_file;
	QByteArray contenuto_file_BA;
	qint64 dimensione_file;
	dimensione_file=file_aperto.size ();
	ui->label_dim->setText ("size="+(QString::number (dimensione_file)));
	contenuto_file_BA=file_aperto.read (dimensione_file);
	//contenuto_file_BA=file_aperto.readAll();
	if (contenuto_file_BA<=0){
     	QMessageBox::critical(this,"Errore","Lettura con esito negativo");
     	ui->label_stato->setText ("errore lettura");
	}
	else{
    	contenuto_file=contenuto_file_BA;
    	ui->plainTextEdit->setPlainText (contenuto_file);
    	MainWindow::setWindowTitle ("Mio_notepad" + percorso_file);
    	ui->label_stato->setText ("lettura OK");
	}

}

2

Monday, November 7th 2011, 4:39pm

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <QApplication>
#include <QFile>
...

int main(int argc, char* argv[])
{
  QApplication app(argc, argv);
  QFile fil("testfile.txt");
  fil.open(QIODevice::WriteOnly | QIODevice::Text);

  qDebug() << "filename " << fil.fileName();
  qDebug() << "handle " << fil.handle();

  fil.write("test\n123");
  fil.close();

  QFile fil_read("testfile.txt");
  fil_read.open(QIODevice::ReadOnly);
  QByteArray arr = fil_read.readAll();

  qDebug() << QCoreApplication::applicationDirPath();
  qDebug() << arr.constData();
}


output:
filename "testfile.txt"
handle 3
"D:/Coding/C++/Projects/qt_test/Debug"
test
123
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar threads