UPD:

class CsvReader: public QObject { Q_OBJECT private: QFile _file; ... } 

In order:

  1. I create an instance of CsvReader
  2. I set the file name for _file:

    _file.setFileName(name);

  3. I open the file:

.

 if(!_file.open(QIODevice::ReadOnly | QIODevice::Text)) { const char* msg = "File is imposible to open!"; emit display_dialog(msg, false, true); qDebug(_file.fileName().toAscii()); qDebug(" - File is imposible to open!\n"); return false; } 

However, it does not open in the output of the application:

 QFile::open: File (D:/QtProjects/foils_build/naca_1.csv) already open D:/QtProjects/foils_build/naca_1.csv - File is imposible to open! 

What is wrong doing?

  • one
    Nothing is clear. Give all the code regarding the creation and opening of _file. But in any case, the text of the error speaks for itself: either the file holds some of your “notepad” and does not allow anyone else to open it, or you open it twice in your code. - cy6erGn0m
  • As for the notebook, you are bent in my opinion :) Most likely, it opens or opens somewhere, but does not close: it seems that for an open QFile you can change the path to the file. - Surendil
  • Except in the program, the file did not open anywhere. In the code opens in a single place. - Alexey Kotov

2 answers 2

Solved.

I did not pay attention to the CsvReader constructor:

 CsvReader::CsvReader(QObject *parent, const QString& file_name): QObject(parent), _file(file_name), _separator(';') {} 

It was there ( _file(file_name) ) file and opened the first time. Simple inattention.

    You can try to replace your code for working with QFile with, for example, working with the default C++ stream and see what happens.

    Also, be setFileName to the mechanics of setFileName :

     void QFile::setFileName ( const QString & name ) 

    Sets the name of the file. The name can have no path, a relative path, or an absolute path. Do not call this function if the file has already been opened.


    It is likely that somewhere your open and setFileName are not exactly what you expected.

    • For the sake of interest, you can try to do without QFile. But I would like all the same comb. Regarding setFileName - the call immediately before open. So it should be? - Alexey Kotov
    • @decodder Try on stream, there can be different reasons. Maybe, after all, somewhere else there is access to your file. I can not say for sure. - Costantino Rupert
    • I will try =) - Alexey Kotov