Reading from Multiple long .txt files in Qt - qt

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.

Related

Access to .h and .cpp files

I am trying to access .h and .cpp files in a selected folder. My goal is to find and print on MainWindow how many lines in .h and .cpp files. In my code, there is no problem to access to selected folder. But for example, what if a .txt file is in this folder, I do not want to count lines of .txt file. My code is here:
int countLine::funcCountLines(QString fullPath)
{
int counter = 0;
QFile file(fullPath);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return 0;
}
while(!file.atEnd())
{
QByteArray line = file.readLine();
counter++;
}
return counter;
}
Best regards!
You can use QDir::setNameFilters to retrieve only the files you are interested in.
QDir dir;
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
// Only find .h and .cpp files
QStringList filters;
filters << "*.cpp" << "*.h";
dir.setNameFilters(filters);
QFileInfoList list = dir.entryInfoList();
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
funcCountLines(fileInfo.fileName());
}

QFile error : device not open

I have a code:
int actualSize = 8;
QFile tableFile("C:\\Users\\Ms\\Documents\\L3\\table"+QString::number(actualSize)+".txt");
QTextStream in(&tableFile);
QString oneLine;
oneLine.append(in.readAll());
if(tableFile.exists())
{
messageLabel->setText(oneLine);
}else
{
messageLabel->setText("Not open");
}
In the C:\Users\Ms\Documents\L3\ folder, I have a "table8.txt" file. But the messageLabel (which is a QLabel) will have a "Not open" text, oneLine is empty, tableFile.exists() is false, and I got a device not open warning/error.
I tried relative path, like
QFile tableFile("table"+QString::number(actualSize)+".txt");
But none of the methods I come up with was good.
You should be able to use / separators for all QFile-related paths. Open the file before you read it and close it when done.
int actualSize = 8;
QFile tableFile("C:/Users/Ms/Documents/L3/table"+QString::number(actualSize)+".txt");
if(tableFile.exists() && tableFile.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&tableFile);
QString oneLine;
oneLine.append(in.readAll());
messageLabel->setText(oneLine);
tableFile.close();
} else
{
messageLabel->setText("Not open");
}

How to write decimal values to csv file in qt

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.

File copy operation does not work in a separate thread?

I am trying to run code of copying files in other thread so that it may not freeze the GUI of the application.
I found that it does not seem to work in a separate thread.
Why is it not working ?
void CopyOperation::run()
{
CopyFilesToFolder(list,sFolder);
}
bool CopyOperation::CopyFilesToFolder(const QStringList &oFileList,const QString
&sTargetFolder)
{
if(sTargetFolder.isEmpty())
{
status = false;
return false;
}
QDir dir(sTargetFolder);
if(!dir.exists()) dir.mkdir(sTargetFolder);
QString sOldDirPath = dir.currentPath();
//if(!dir.setCurrent(sTargetFolder)) return false;
QFile file;
status = true;
foreach(QString sFileName,oFileList)
{
file.setFileName(sFileName);
QFileInfo info(sFileName);
QString newfile = sTargetFolder + "/" + info.fileName();
qDebug() << "\n name = " << newfile;
if(!QFile::copy(sFileName,newfile))
//if(!file.copy(newfile))
{
qDebug() << "\n File copy failed .. " + file.fileName() + " Error : " + file.errorString();
status = false;
break;
}
}
qDebug() << "\n Result .. " << file.errorString() << "code " << file.error();
//dir.setCurrent(sOldDirPath);
return status;
}
Since you didn't post code, I just can try to guess what is the problem. Qt has a static function:
bool copy ( const QString & fileName, const QString & newName )
There is also a copy which is not static:
bool copy ( const QString & newName )
Both of them will fail if file defined by newName already exists, ie. existing file will not be overwritten. Also, maybe path doesn't exists. Without some portion of code is difficult to guess what is the problem.

Qt - reading from a text file

I have a table view with three columns; I have just passed to write into text file using this code
QFile file("/home/hamad/lesson11.txt");
if(!file.open(QIODevice::WriteOnly)) {
QMessageBox::information(0,"error",file.errorString());
}
QString dd;
for(int row=0; row < model->rowCount(); row++) {
dd = model->item(row,0)->text() + ","
+ model->item(row,1)->text() + ","
+ model->item(row,2)->text();
QTextStream out(&file);
out << dd << endl;
}
But I'm not succeed to read the same file again, I tried this code but I don't know where is the problem in it
QFile file("/home/hamad/lesson11.txt");
QTextStream in(&file);
QString line = in.readLine();
while(!in.atEnd()) {
QStringList fields = line.split(",");
model->appendRow(fields);
}
Any help please ?
You have to replace string line
QString line = in.readLine();
into while:
QFile file("/home/hamad/lesson11.txt");
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}
QTextStream in(&file);
while(!in.atEnd()) {
QString line = in.readLine();
QStringList fields = line.split(",");
model->appendRow(fields);
}
file.close();

Resources