I have written a piece of code that should save doubles in an csv file. Here it is:
QString fileName = QFileDialog::getSaveFileName(this,tr("Save Logger Data"), "",tr("LoggerData(*.csv);;All Files (*)"));
if (fileName.isEmpty())
{
return;
}
else
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
{
QMessageBox::information(this, tr("Unable to open file"), file.errorString());
return;
}
QDataStream out(&file);
out << data1 << "/t" << data2 << "/n";
}
Here, data1 and data2 are doubles. When I open the savefile I only see weird characters (I asume they are hexadecimal values??). How can I change my code so it saves doubles instead of hex?
QDataStream is not the right class for this. For text output use QTextStream instead.
Related
So I wrote a Qt quick application that takes user inputs and stores them in a json file. I now want to add a feature that lets me recall the data in my file and display it in a text field within my application. I can get the text in the C++ portion of my application, Im just not sure how to display it in my user interface. Here is the code to get the text from my json file.
void Jsonfile:: display(){
//1. Open the QFile and write it to a byteArray and close the file
QFile file;
file.setFileName("data.json");
if(!file.open(QIODevice::ReadOnly)){
qDebug() << "file couldn't be opened/found";
return;
}
QByteArray byteArray;
byteArray = file.readAll();
file.close();
//2. Format the content of the byteArray as QJsonDocument
//and check on parse Errors
QJsonParseError parseError;
QJsonDocument jsonDoc;
jsonDoc = QJsonDocument::fromJson(byteArray, &parseError);
if(parseError.error != QJsonParseError::NoError){
qWarning() << "Parse error at " << parseError.offset << ":" << parseError.errorString();
return;
}
QTextStream textStream(stdout);
textStream << jsonDoc.toJson(QJsonDocument::Indented);
}
I'm trying to create a text file by clicking on a button as following code, but I'm not getting.
QString local = "/local/flash/root";
QString name = " ProductionOrder.txt";
void page1000::on_pushButton_3_clicked()
{
QFile file(local+name);
if(!file.open(QFile::WriteOnly|QFile::Text)){
QMessageBox::warning(this,"ERROR","Error open file");
}
QTextStream output(&file);
QString text=ui->plainTextEdit->toPlainText();
output << text;
file.flush();
file.close();
}
what could be wrong with the code?
I am working with Qt 4.8
I don't know what to do
just put "/" at the end of the QString local
Like this: QString local = "/local/flash/root/";
I am beginner in UI design using Qt. My project now is doing comparison. For example: if I have 2 text file.
How can I compare the number line by line? Because I have so many text file like this, and I need compare them on by one.What I can do now is only read the text file by line order. Thank you so much!
The procedure is simple
Read both files (always make sure they are opened successfully)
Read files line by line and convert strings to numbers for comparison.
Quit if there is no data left.
Moreover, you need to make sure that the format of files is consistent otherwise, you need to make sure what you manipulate is a real number. I assume numbers are integers but of course you can change it. Extra precautions are required in this kind of project. I will leave it to you. The simplified code for the above procedure is
#include <QString>
#include <QFile>
#include <QDebug>
#include <QTextStream>
int main()
{
QFile data1("text1.txt");
if (!data1.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "text1.txt file can't be opened...";
return -1;
}
QFile data2("text2.txt");
if (!data2.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "text2.txt file can't be opened...";
return -1;
}
QTextStream in1(&data1), in2(&data2);
while ( !in1.atEnd() && !in2.atEnd() ) {
QString num1 = in1.readLine();
QString num2 = in2.readLine();
if ( num1.toInt() > num2.toInt() )
qDebug() << num1.toInt() << ">" << num2.toInt();
// do the rest of comparison
}
return 0;
}
Now in my case, the txt files are
text1.txt
1
2
3
4
text2.txt
3
5
1
6
The output is
3 > 1
Edit: the OP is looking for the difference and its sum.
int sum(0);
while ( !in1.atEnd() && !in2.atEnd() ) {
QString num1 = in1.readLine();
QString num2 = in2.readLine();
int result = num1.toInt() - num2.toInt();
qDebug() << num1.toInt() << "-" << num2.toInt() << " = " << result;
sum += result;
}
qDebug() << "sum = " << sum;
Basic approach would be something like this:
QString filename1("C:/Users/UserName/Downloads/t1.txt");
QString filename2("C:/Users/UserName/Downloads/t2.txt");
QFile file(filename1);
file.open(QIODevice::ReadOnly);
QTextStream in(&file);
QStringList textOfFile1;
while (!in.atEnd()) {
QString line = in.readLine();
textOfFile1.append(line);
}
QFile file2(filename2);
file2.open(QIODevice::ReadOnly);
QTextStream in2(&file2);
QStringList textOfFile2;
while (!in.atEnd()) {
QString line = in.readLine();
textOfFile2.append(line);
}
if(textOfFile1.size() != textOfFile2) return false;
for(int i = 0; i < textOfFile1.size(); i++)
{
if(textOfFile1[i] != textOfFile2[i]) return false;
}
return true;
i.e. You read the files into a QStringList and you compare the lists line by line. This way you can also catch the firs # of line where there was a mismatch. Note that such comparison also considers white spaces such as \n \t etc.
PS: wrap the readers into functions, to avoid duplication like me. :)
Hope this helps ;)
I am trying to read data from Multiple files in Qt.
This how I am doing it:
void MainWindow::on_pushButton_3_clicked()
{
QString path = "C:/MyDevelopment/readfiles";
QDir dir(path);
QStringList filters;
filters << "*.txt";
foreach ( QString fileName, dir.entryList(filters, QDir::Files) )
{
QFile readFile(fileName);
if(!readFile.open(QIODevice::ReadOnly | QIODevice::Text ) )
{
qDebug("Failed to read file.....");
//return ;
}
QTextStream in(&fileName);
while (!in.atEnd())
{
QString line = in.readLine();
qDebug() << line;
}
}
it is always going in failed to open. what i am doing wrong here??
in mentioned directory all files are .txt files.
I want to write a QString in a textfile in a ziparchive with QuaZip. I use Qt Creator on WinXP. With my code the text-file in the archive is created but empty.
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
QTextStream ts ( &file );
ts << doc.toString();
file.close();
zipfile.close();
When I try with a QFile it works as expected:
QDomDocument doc;
/* doc is filled with some XML-data */
QFile file("test.xml");
file.open(QIODevice::WriteOnly);
QTextStream ts ( &file );
ts << doc.toString();
file.close();
I find the right content in test.xml, so the String is there, but somehow the QTextStream doesn't want to work with the QuaZipFile.
When I do it with a QDataStream instead of QTextStream there is an output, but not a correct one.
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
QDataStream ts ( &file );
ts << doc.toString();
file.close();
zipfile.close();
The foo.xml in the test.zip is filled with some data, but wrong formatted (between each character is an extra 'nul'-character).
How can I write the String in the textfile in the zip-archive?
Thanks,
Paul
You don't need QTextStream or QDataStream to write a QDomDocument to a ZIP file.
You can simply do the following:
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
// After .toString(), you should specify a text codec to use to encode the
// string data into the (binary) file. Here, I use UTF-8:
file.write(doc.toString().toUtf8());
file.close();
zipfile->close();
In the original first example you must flush the stream:
QDomDocument doc;
/* doc is filled with some XML-data */
zipfile = new QuaZip("test.zip");
zipfile->open(QuaZip::mdCreate);
QuaZipFile file(zipfile);
file.open(QIODevice::WriteOnly, QuaZipNewInfo("foo.xml"));
QTextStream ts ( &file );
ts << doc.toString();
ts.flush();
file.close();
zipfile.close();