L'esempio e' ripreso sostanzialmente dall'help in linea per cui non vengono fornite ulteriori spiegazioni
------------------------------------------------
//scrittura
QFile mfile("/home/luca/prova.txt");
if (!mfile.open(QFile::WriteOnly | QFile::Text))
{
return ;
}
QTextStream out(&mfile);
out << "La risposta e': " << 42 << "\n";
out << "Ma la domanda quale e'??";
mfile.flush();
mfile.close();
//lettura
QFile rfile("/home/luca/prova.txt");
if (!rfile.open(QFile::ReadOnly | QFile::Text))
return;
QTextStream in(&rfile);
QString t;
while (!in.atEnd()) {
QString line = in.readLine();
t = t + line + "\n";
}
ui->textEdit->setText(t);
rfile.close();
-------------------------------------------------
altrimenti in C++ classico
--------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ofstream myfile ("prova.txt");
if (myfile.is_open()){
myfile << "This is a line.\n";
myfile.close();
}
else
cout << "Unable to open file";
ifstream myfile2 ("prova.txt");
if (myfile2.is_open())
{
while (! myfile2.eof() )
{
getline (myfile2,line);
cout << line << endl;
}
myfile2.close();
}else
cout << "Unable to open file";
}
--------------------------------------------------
Nessun commento:
Posta un commento