Not able to reproduce the notepad example in qt; read access violation - c++

I'm learning Qt following this link.
When trying to open a file through function member open I get the error:
Exception thrown: read access violation.
this was 0xFFFFFFFFFFFFFFFF.
This is the definition of the function:
Notepad::open(){
QString fileName = QFileDialog::getOpenFileName(this, "Open File", "", "Text Files (*.txt);;C++ Files (*.cpp *.h");
if (fileName != "") {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(this, "Error", "Could not open file");
}
QTextStream in(&file);
textEdit->setText(in.readAll());
//textEdit->setText("test 1a2b");
file.close();
}
}
Can anybody see the problem?

Related

Qt, C++ - Editing an existing INI file from project doesn't work when I need to save it [duplicate]

It's strange, I add desired file into the resources via Add Existing Files..., the file is there. I run qmake ("Build->Run qmake") to make the file available.
The first issue: I can't write anything into the file from output terminal! But when I manually write into the file, the output terminal shows the change every time I run it. Second issue: it still says QIODevice::read: device not open !
Here's my code:
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QString>
#include <QTextStream>
#include <iostream>
void wFile(QString Filename)
{
QFile nFile(Filename);
QTextStream str(&nFile);
qDebug() << "what do you want to write in the desired file: ";
str.readLine();
if (!nFile.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << "could not open the file";
return;
}
nFile.flush();
nFile.close();
}
void read (QString Filename){
QFile nFile(Filename);
if(!nFile.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << "could not open file for reading";
return;
}
QTextStream in(&nFile);
QString nText = in.readAll();
qDebug() << nText;
nFile.close();
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString nFilename =":/MyFiles/DocumentArminV.txt";
wFile(nFilename);
read(nFilename);
return a.exec();
}
And here's output terminal of the code:
The files saved in a qresource are read-only since they are part of the executable so you can not write or modify them.
docs:
Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. ...

Opening and reading a file in Qt

I am trying to use the menu bar to open a file (hardcoded) when 'File|Open' is clicked. The file should append to and display each line. My function is not finding the file. So after I click open I am getting back 'trace.txt cannot be found'. I have the file saved in the same directory as the rest of the project files. I am wondering if I haven't opened the file properly? Can anyone have a look at my code and see if you're catching an error that I am not?
void MainWindow::readFile(){
infoLabel->setText(tr("Invoked <b>File|Open</b>"));
QString filename="trace.txt";
QFile file(filename);
if(!file.exists()){
qDebug() << "File <i>cannot</i> be found "<<filename;
}else{
qDebug() << filename<<" Opening...";
}
QString line;
textEdit->clear();
if (file.open(QIODevice::ReadOnly | QIODevice::Text)){
QTextStream stream(&file);
while (!stream.atEnd()){
line = stream.readLine();
textEdit->setText(textEdit->toPlainText()+"0x"+line+"\n");
qDebug() << "line: "<<line;
}
}
file.close();
}
UPDATE:
I changed the QFile object to the direct path and that found the file. On the other hand, I am reading it in an infinite loop, which never makes it to the textEdit and continually outputs to the debugger. Any ideas?
Use current or currentPath() to see with which directory you are working.
See this example to understand the current directory:
QFile file;
QDir::setCurrent("/tmp");
file.setFileName("readme.txt");
QDir::setCurrent("/home");
file.open(QIODevice::ReadOnly); // opens "/home/readme.txt" under Unix
From http://doc.qt.io/qt-5/qfile.html#QFile

Why qt does not read a file

I have the following lines of code:
QFile file("1.txt");
qDebug() << file.readAll();
The file is locating in project directory. When I compile the code, I get:
"QIODevice::read (QFile, "1.txt"): device not open"
You must open the file before reading.
As a starting point (from the documentation):
QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}
Other options can be found here.

Qt select existing file

I am new to Qt and I want to be able to select a already existing file called default.ini which is in the same directory as the exe. I already have some code that allows me to do that, but the user has to manually select the file everytime.
QString file = QFileDialog::getOpenFileName(this, tr("Open File"), "/debug", tr("default (*.ini)"));
if (file != NULL) {
try {
controller_->ConfigurationFileSave(file.toStdString());
} catch (std::exception &e) {
Logger::Log(std::string("Failed to save configuration: ") + e.what(),
Logger::kError);
}
}
The program does everything I want it to do in terms of writing/ reading to the file, I just don't want the program to require any user input in opening the file. I understand that the reason I have user input is because I am using the QFileDialog class and I just want to know if there is another class that does it automatically. Thanks
Edit 1 As per Arun's suggestion I tried to use Qfile. The program now reads from the default.ini config file successfully but it won't save to the config file. Is there an easy way to write to the file?
QFile file("default.ini");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QString content = file.readAll();
file.close();
if (content != NULL) {
try {
controller_->ConfigurationFileSave(content.toStdString());
} catch (std::exception &e) {
Logger::Log(std::string("Failed to save configuration: ") + e.what(),
Logger::kError);
}
}
Edit 2 As per Arun's second suggestion:
QFile file("default.ini");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QString line = file.readAll();
file.close();
file.open(QIODevice::WriteOnly | QIODevice::Text);
try {
controller_->ConfigurationFileSave(line.toStdString());
} catch (std::exception &e) {
Logger::Log(std::string("Failed to save configuration: ") + e.what(),
Logger::kError);
}
file.close();
Your file chooser code was passing a filename to ConfigurationFileSave(). Here's code to get that file name without interaction:
QDir appDir(QCoreApplication::applicationDirPath());
QFileInfo file(appDir, "default.ini");
// file.filePath() or file.absoluteFilePath() here:
controller_->ConfigurationFileSave( ___HERE___ );
You can test this in QtCreator by creating a new console application and using this as your main.c:
#include <QCoreApplication>
#include <QDebug>
#include <QFileInfo>
#include <QDir>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QDir appDir(QCoreApplication::applicationDirPath());
QFileInfo file(appDir, "default.ini");
qDebug() << " " << file.filePath();
qDebug() << " " << file.absoluteFilePath();
return 0;
}
Output:
silver:qt_app_dir hchapman$ ls
Makefile main.cpp main.o qt_app_dir qt_app_dir.pro qt_app_dir.pro.user
silver:qt_app_dir hchapman$ ./qt_app_dir
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
silver:qt_app_dir hchapman$ cd ..
silver:Desktop hchapman$ ./qt_app_dir/qt_app_dir
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
"/Users/hchapman/Desktop/qt_app_dir/default.ini"
silver:Desktop hchapman$
Here is a typical example of how you can do file I/O without using QFileDialog.
This example here uses QFile
QFile file("default.ini");
if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
return;
while (!file.atEnd()) {
QByteArray line = file.readLine();
process_line(line);
}

QFile won't open the file

I have a program that I basically stole from the Qt website to try to get a file to open. The program refuses to open anything I am confused as to why. I have looked for lots of documentation but found nothing can you please explain why it does not work.
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QString>
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent)
{
QFile file("C:/n.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&file);
QString f=in.readLine();
lab =new QLabel("error",this);
lab->setGeometry(100,100,100,100);
lab->setText(f);
}
Before opening the file, you can always check the existense:
QFile file("myfile.txt");
if (!file.exists()) {
// react
}
If file exists but does not open, you can get the error state and message:
QString errMsg;
QFileDevice::FileError err = QFileDevice::NoError;
if (!file.open(QIODevice::ReadOnly)) {
errMsg = file.errorString();
err = file.error();
}
And always: if the file was openend, then remember to close it. In your example you didn't:
file.close();
FileError QFile::error () const
Returns the file error status.
The I/O device status returns an error code. For example, if open() returns false, or a read/write operation returns -1, this function can be called to find out the reason why the operation failed.
See also unsetError().
Post the error code. Isn't it supposed to be QFile file("C:\n.txt"); \ not / in windows?