Newline character into textfile using QFile - qt

i want to put a newline into a txt file, i tried with many alternative with so many help from this forum but i am getting always unknown character always. Please help
for (int i = 0; i < fileDet.size(); i++) {
qDebug() << "Name directory" << fileDet.at(i);
QFile data("output.txt");
if (data.open(QFile::Append)) {
QTextStream out(&data);
out << fileDet.at(i); //<<'\n';
out << QChar((int)'\n');
}
data.close();
}

Try out << endl;

When you open the file:
data.open(QFile::Append | QFile::Text)

Related

How can I get required line?

I wrote code, that takes output from QProcess to QTextStream, than show line that I need:
QProcess p;
p.start("fdisk",QStringList() << "/dev/sdb" << "-l");
p.waitForFinished();
QString processOutput = p.readAll();
QTextStream processOutputTextStream(&processOutput);
QString line;
while(!processOutputTextStream.atEnd()){
line = processOutputTextStream.readLine();
if(line.contains("Disk /dev/sdb:")){
qDebug() << line;
}
}
The output:
"Disk /dev/sdb: 28.67 GiB, 30765219840 bytes, 60088320 sectors"
I need only last numbers (60088320)
How to do this?
auto words = line.split(" "); auto number_str = words.at(words.length() - 2);
Thanks to eyllanesc

How to go from QByteArrey to a byte pointer in Qt

I want to convert my QByteArrey into a byte pointer. I found out on the internet how to go the opposite way and i found myself struggling in finding the right way to do it... So can anyone help me please?
Use data() or constData() method.
QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
cout << "[" << *data << "]" << Qt::endl;
++data;
}

Calling a method from a different .cpp file

I want to save some data as a text file, the first txt file will contain header information, the other text file will save data streamed from sensors, so with the help from the internet I created the following "datalogger.cpp" file
#include "datalogger.h"
#include <QDebug>
#include <iostream>
#include <QFile>
DataLogger::DataLogger(QObject *parent) : QObject(parent)
{
}
DataLogger::~DataLogger(){
}
void DataLogger::save(DataStream &input){
saveAsText(input);
}
void DataLogger::saveAsText(DataStream &input){
QTextStream outHeader(&outFileHeader);
outHeader << "[CAPTURE SETTINGS]\n"
<< "Filename: " << SettingsSingleton::instance().getFileName() << ".txt \n"
<< "Samples: " << QString::number(input.size()) << "\n"
<< "Duration: " << QString::number(input.back().time) << "ms \n"
<< "Sample rate: " << QString::number(SettingsSingleton::instance().getSampleRate()) << " Hz\n"
<< "Source: " << SettingsSingleton::instance().getSource() << "\n"
outFileHeader.close();
}
QFile outFile(SettingsSingleton::instance().getFileName() + ".txt");
QTextStream out(&outFile);
for (int i ; i<input.size();i++){
const EcgStreamObject tmp=input.at(i);
out << tmp.toText() << endl; //"\n";
}
outFile.close();
}
}
I have my "DataStream" input variable that I want to pass to the method and save as a ".txt" file, however I do no know how to call the method "void DataLogger::save(DataStream &input)" from a different ".cpp" file where the DataStream variable is located.
I am extremely new to c++ please it as simple as possible please.
Thank you in advance
If I get your question right, it is about how to create and use a header file in c++.
There is already a lot of information about this, for example this article from cplusplus.com or this from learncpp.com.
All you should need to do is add "datalogger.h" to your other file.
When you call the method DataLogger::save from your other file, the program will search for an implementation that was linked to your program (i.e. the file you posted here) and use that to actually perform the work.
Stefan's answer is probably important to look at too as it will cover this case and some other basics if you are going to be working in C/C++ long term.

Read exif metadata of images in Qt

In my Qt app I want to read exif data of images. QImage or QPixmap don't seem to provide such hooks.
Is there any API in Qt that allows reading exif without using external libraries like libexif?
EDIT: This is a duplicate of this
For me, the best choice was easyexif by Mayank Lahiri. You only need to add two files exif.cpp and exif.h to your project.
int main(int argc, char *argv[])
{
for (int i=1; i<argc; ++i){
QFile file(argv[i]);
if (file.open(QIODevice::ReadOnly)){
QByteArray data = file.readAll();
easyexif::EXIFInfo info;
if (int code = info.parseFrom((unsigned char *)data.data(), data.size())){
qDebug() << "Error parsing EXIF: code " << code;
continue;
}
qDebug() << "Camera model : " << info.Model.c_str();
qDebug() << "Original date/time : " << info.DateTimeOriginal.c_str();
} else
qDebug() << "Can't open file:" << argv[i];
}
return 0;
}
Try QExifImageHeader from qt extended framework. qtextended.org is not available for me? but you may search for other download mirrows.
QImageReader has a method named transformation() which is introduced in version 5.5, first you should try that.
You can also check the following link to see how it's done using Windows GDI in Qt, http://amin-ahmadi.com/2015/12/17/how-to-read-image-orientation-in-qt-using-stored-exif/

QList memory deallocation

I'm trying to free memory after using QList, but it doesn't seem to work properly.
Here's my code:
QList<double> * myList;
myList = new QList<double>;
double myNumber;
cout << "CP1" << endl;
getchar(); // checkpoint 1
for (int i=0; i<1000000; i++)
{
myNumber = i;
myList->append(myNumber);
cout << myList->size() << endl;
}
cout << "CP2!" << endl;
getchar(); // checkpoint 2
for (int i=999999; i>0; i--)
{
myList->removeLast();
cout << myList->size() << endl;
}
cout << "CP3!" << endl;
getchar(); // checkpoint 3
delete myList;
cout << "CP4!" << endl;
getchar(); // checkpoint 4
Memory usage:
CP1: 460k
CP2:19996k
CP3:19996k
CP4:16088k
So it looks like despite removing of elements and deleting myList still large part of memory is being used. I believe there is a way to handle it but I can't find it.
Thanks in advance for any help.
Pawel
Memory manager is not required to release the memory your program has allocated. There are no problems in your deallocation.
QList is an array based list. The array expands automatically, but does not shrink automatically. Removing elements from the list does not affect the size of the array.
To trim the array down to the actual size, create a new QList and add the contents to it. Then delete the original list.
Unfortunately looks like there is no convenience method to do this, like the List.TrimExcess() in .NET.

Resources