QT FILE OPERATION
-
wrote on 16 Jan 2021, 10:35 last edited by swansorter
sir
I am working on Nano pc t2 board
pc os ubuntu 16.04
Nano pc t2 OS 16.04 ubuntu
i am able to do all the file operation (open read close and write )in pc,but if i cross compile and run it on my kit(nano pc t2) file operation are not working#include "mainwindow.h" #include "ui_mainwindow.h" #include<qfile.h> #include <QTextStream> #include <QtCore> QFile mainsort(QDir::current().path()+"/ms.csv"); QByteArray temp1; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_pressed() { if ( mainsort.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text) ) { QTextStream out(&mainsort); out << "something"; ui->label->setText("WRitten"); } mainsort.close(); } void MainWindow::on_pushButton_2_pressed() { QCoreApplication::quit(); } void MainWindow::on_pushButton_3_pressed() { mainsort.open(QIODevice::ReadOnly); temp1=mainsort.readAll(); mainsort.close(); ui->label->setText( QString::fromUtf8(temp1)); }
-
@JonB yea
wrote on 18 Jan 2021, 08:57 last edited by JonB@swansorter
Slightly strange that you just get a number, and it seems to mean I/O Error, which is also a touch strange.Nevertheless, @jsulm has told you above what you need to use. Pick a
QStandardPaths
suitable for the user to have write permission, for exampleQStandardPaths::HomeLocation
.3.i wanted place file where my application file is located
This is a bad idea. The application file/executable is likely to be placed in a directory to which the user does not have write access, and anyway it's not a good place for data files.
-
sir
I am working on Nano pc t2 board
pc os ubuntu 16.04
Nano pc t2 OS 16.04 ubuntu
i am able to do all the file operation (open read close and write )in pc,but if i cross compile and run it on my kit(nano pc t2) file operation are not working#include "mainwindow.h" #include "ui_mainwindow.h" #include<qfile.h> #include <QTextStream> #include <QtCore> QFile mainsort(QDir::current().path()+"/ms.csv"); QByteArray temp1; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_pressed() { if ( mainsort.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text) ) { QTextStream out(&mainsort); out << "something"; ui->label->setText("WRitten"); } mainsort.close(); } void MainWindow::on_pushButton_2_pressed() { QCoreApplication::quit(); } void MainWindow::on_pushButton_3_pressed() { mainsort.open(QIODevice::ReadOnly); temp1=mainsort.readAll(); mainsort.close(); ui->label->setText( QString::fromUtf8(temp1)); }
wrote on 16 Jan 2021, 11:45 last edited byFrom your code we don't even know what mainsort is. Probably a file, but where it is and how you try to open it, remain mysteries. Perhaps there is relative path issue.
-
From your code we don't even know what mainsort is. Probably a file, but where it is and how you try to open it, remain mysteries. Perhaps there is relative path issue.
wrote on 16 Jan 2021, 13:03 last edited by@mvuori mainsort is file name,it is in poject directory path
-
@mvuori mainsort is file name,it is in poject directory path
@swansorter but the executable is in the build directory, not the project directory.
Regards
-
@swansorter but the executable is in the build directory, not the project directory.
Regards
wrote on 16 Jan 2021, 13:10 last edited by@aha_1980 In pc it working fine .In buid directory executable is there and my mainsort file also there only.
but it is not happening in my board
-
@aha_1980 In pc it working fine .In buid directory executable is there and my mainsort file also there only.
but it is not happening in my board
wrote on 16 Jan 2021, 13:32 last edited by JonB@swansorter
What is not happening on board? Where is the file located, and how are you expecting it to be picked up with the path you specify? What are you expectingQDir::current()
to be in each case? Why don't you at least output it in each case so they you/we know where it is looking? And why doesn't your code give any indication to anybody if it fails to open the file? And no call toQFile::error
/errorString()
.Also, personally, I would not try to access
QDir::current()
at global scope. It will presumably be evaluated prior to whateverQApplication
you create. Wouldn't it be safer to have it execute that after creating the application? There is no need to have those two variables as globals. -
@swansorter
What is not happening on board? Where is the file located, and how are you expecting it to be picked up with the path you specify? What are you expectingQDir::current()
to be in each case? Why don't you at least output it in each case so they you/we know where it is looking? And why doesn't your code give any indication to anybody if it fails to open the file? And no call toQFile::error
/errorString()
.Also, personally, I would not try to access
QDir::current()
at global scope. It will presumably be evaluated prior to whateverQApplication
you create. Wouldn't it be safer to have it execute that after creating the application? There is no need to have those two variables as globals.wrote on 18 Jan 2021, 07:17 last edited by@JonB
1.file creation not happening
2.located in opt/QtE_demo /
3.i wanted place file where my application file is located -
@JonB
1.file creation not happening
2.located in opt/QtE_demo /
3.i wanted place file where my application file is located@swansorter said in QT FILE OPERATION:
opt/QtE_demo /
Do you mean /opt/QtE_demo/? If so /opt is not writeable by normal users.
-
@swansorter said in QT FILE OPERATION:
opt/QtE_demo /
Do you mean /opt/QtE_demo/? If so /opt is not writeable by normal users.
wrote on 18 Jan 2021, 07:37 last edited by@jsulm then where i can place ?
-
@jsulm then where i can place ?
@swansorter User data is usually stored in user home directory.
Check https://doc.qt.io/qt-5/qstandardpaths.html to see how to get various standard locations. -
@JonB
1.file creation not happening
2.located in opt/QtE_demo /
3.i wanted place file where my application file is locatedwrote on 18 Jan 2021, 07:40 last edited by@swansorter said in QT FILE OPERATION:
1.file creation not happening
What did you do about printing
QFile::error
/errorString()
if file open fails? -
@swansorter said in QT FILE OPERATION:
1.file creation not happening
What did you do about printing
QFile::error
/errorString()
if file open fails?wrote on 18 Jan 2021, 08:02 last edited by@JonB getting error 5
-
@swansorter User data is usually stored in user home directory.
Check https://doc.qt.io/qt-5/qstandardpaths.html to see how to get various standard locations.wrote on 18 Jan 2021, 08:03 last edited by@jsulm i will check
-
@JonB getting error 5
wrote on 18 Jan 2021, 08:06 last edited by JonB@swansorter said in QT FILE OPERATION:
@JonB getting error 5
That is what
QFile::errorString()
reports? -
@swansorter said in QT FILE OPERATION:
@JonB getting error 5
That is what
QFile::errorString()
reports?wrote on 18 Jan 2021, 08:33 last edited by@JonB yea
-
sir
I am working on Nano pc t2 board
pc os ubuntu 16.04
Nano pc t2 OS 16.04 ubuntu
i am able to do all the file operation (open read close and write )in pc,but if i cross compile and run it on my kit(nano pc t2) file operation are not working#include "mainwindow.h" #include "ui_mainwindow.h" #include<qfile.h> #include <QTextStream> #include <QtCore> QFile mainsort(QDir::current().path()+"/ms.csv"); QByteArray temp1; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_pressed() { if ( mainsort.open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text) ) { QTextStream out(&mainsort); out << "something"; ui->label->setText("WRitten"); } mainsort.close(); } void MainWindow::on_pushButton_2_pressed() { QCoreApplication::quit(); } void MainWindow::on_pushButton_3_pressed() { mainsort.open(QIODevice::ReadOnly); temp1=mainsort.readAll(); mainsort.close(); ui->label->setText( QString::fromUtf8(temp1)); }
wrote on 18 Jan 2021, 08:45 last edited by KroMignon@swansorter said in QT FILE OPERATION:
QFile mainsort(QDir::current().path()+"/ms.csv");
I don't think this is a good idea to do this, at least with a global variable!
Have you checked, on the Nano pc on which path you tried to write?
Have you checked the directory is writable / the application is allowed to write there? -
@JonB yea
wrote on 18 Jan 2021, 08:57 last edited by JonB@swansorter
Slightly strange that you just get a number, and it seems to mean I/O Error, which is also a touch strange.Nevertheless, @jsulm has told you above what you need to use. Pick a
QStandardPaths
suitable for the user to have write permission, for exampleQStandardPaths::HomeLocation
.3.i wanted place file where my application file is located
This is a bad idea. The application file/executable is likely to be placed in a directory to which the user does not have write access, and anyway it's not a good place for data files.
-
@swansorter
Slightly strange that you just get a number, and it seems to mean I/O Error, which is also a touch strange.Nevertheless, @jsulm has told you above what you need to use. Pick a
QStandardPaths
suitable for the user to have write permission, for exampleQStandardPaths::HomeLocation
.3.i wanted place file where my application file is located
This is a bad idea. The application file/executable is likely to be placed in a directory to which the user does not have write access, and anyway it's not a good place for data files.
wrote on 19 Jan 2021, 04:46 last edited by@JonB thank very much.
i created separate folder for file operation in home location -
@swansorter said in QT FILE OPERATION:
QFile mainsort(QDir::current().path()+"/ms.csv");
I don't think this is a good idea to do this, at least with a global variable!
Have you checked, on the Nano pc on which path you tried to write?
Have you checked the directory is writable / the application is allowed to write there?wrote on 19 Jan 2021, 04:47 last edited by@KroMignon thank you sir for your information
3/19