phillip_Qt
22nd June 2010, 11:26
Hi ALL,
I'm trying to create a file and update some data in the file.
e.g Data is like
Helllo
Hi
Qt
Test
Below is my code. but last line is overwriting. Ineed new text to be insert a new line and append the string. Plz tell me how to correct this code snipset.
#include <QtCore/QCoreApplication>
#include <iostream>
#include <qfile.h>
#include <qtextstream.h>
using namespace std;
void File(QString myFilename, QString myData)
{
QFile file(myFilename);
if ( file.open( QIODevice::ReadWrite) )
{
QTextStream stream( &file );
// while (stream.atEnd())
// {
// stream << QChar((int)'\n');
//
// }
stream <<myData << endl;
}
else
{
qDebug( "Could not create file %s" );
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString file("D:\\test.txt");
File(file, "1St Line: Hello");
File(file, "2Nd Line : Hi");
File(file, "3rd Line : Qt");
File(file, "4th Line : Test");
return a.exec();
}
Thank u all,
Phillip
I'm trying to create a file and update some data in the file.
e.g Data is like
Helllo
Hi
Qt
Test
Below is my code. but last line is overwriting. Ineed new text to be insert a new line and append the string. Plz tell me how to correct this code snipset.
#include <QtCore/QCoreApplication>
#include <iostream>
#include <qfile.h>
#include <qtextstream.h>
using namespace std;
void File(QString myFilename, QString myData)
{
QFile file(myFilename);
if ( file.open( QIODevice::ReadWrite) )
{
QTextStream stream( &file );
// while (stream.atEnd())
// {
// stream << QChar((int)'\n');
//
// }
stream <<myData << endl;
}
else
{
qDebug( "Could not create file %s" );
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString file("D:\\test.txt");
File(file, "1St Line: Hello");
File(file, "2Nd Line : Hi");
File(file, "3rd Line : Qt");
File(file, "4th Line : Test");
return a.exec();
}
Thank u all,
Phillip