-1

So, I get infinite cycle while trying to read lines from file (line by line). I was trying to use do{}while(); cycle like that:

QTextStream stream(stdin);
QString line;
do {
    line = stream.readLine();
} while (!line.isNull()); 

but I get empty string.

Sure, I checked file path (it is right). I was trying to use /Users/user/tts.txt path but without changes. I was trying to read other files (like m3u). And it's not working on macOS Catalina, Windows 10, Linux (Debian).

So, why did I get infinite cycle?

QStringList Manager::GetLinesFromFile(const QString &nameOfFile)
{
    QStringList lines = {};

    //path to file
    const QString path = QCoreApplication::applicationDirPath() + "/bin/" + "tts.txt";
    //"/Users/user/tts.txt"

    QFile buffer;
    buffer.QFile::setFileName(path);

    #ifndef Q_DEBUG
    qDebug() << path;
    #endif

    if(buffer.QFile::exists())
    {
        if(!buffer.QIODevice::open(QIODevice::ReadOnly))
        {
            #ifndef Q_DEBUG
            qCritical() << "error: can't open file";
            #endif
        }
        else
        {
            QTextStream stream(&buffer);

             // both conditions
            // (!stream.QTextStream::atEnd()) 
            while(!buffer.QFileDevice::atEnd())
                lines.QList::push_back(stream.QTextStream::readLine());

            buffer.QFile::close();
        }
    }
    else
    {
        #ifndef Q_DEBUG
        qCritical() << "error: file not exists";
        #endif
    }

    return lines;
}
bogdyname
  • 358
  • 2
  • 10

2 Answers2

0

Have a look at the QTextstream documentation https://doc.qt.io/qt-5/qtextstream.html. There is an example of reading line by line. Your while loop should read until the stream reaches the end of the buffer and many of the in built read functions will return false when his happens

Kieran
  • 224
  • 1
  • 6
  • yeah, I was trying to make like `QString line; while (stream.readLineInto(&line)) { ... }` but it's not working. And I was trying to use `QTextStream::atEnd()` method. – bogdyname Jun 01 '20 at 11:12
  • I got it. Check my answer. It was not working with any file. – bogdyname Jun 01 '20 at 11:39
  • Ah, looks like I was typing the comment about that as you found it on your own – Kieran Jun 01 '20 at 11:41
0

So, I got it. I opened the file incorrectly.

I was using:

if(!file.QIODevice::open(QIODevice::ReadOnly))

but it should be like that:

if(!file.QFile::open(QFile::ReadOnly))

bogdyname
  • 358
  • 2
  • 10
  • You do not need to specify the class all the time, you already have an instance of the class. Simply do `file.open(QFile::ReadOnly)` – GazTheDestroyer Jun 01 '20 at 11:33
  • @GazTheDestroyer I know. I am writing **name of class** because this code in big project and I need to write **name of class** to find out which class this method is from. – bogdyname Jun 01 '20 at 11:39
  • You should not, because you will introduce errors by calling the wrong class method, exactly as you have done above, and you may override virtual methods. If you are using a decent IDE, you can just double click any variable and eg "Go to Definition" in VS to see the variable type. – GazTheDestroyer Jun 01 '20 at 11:42
  • @GazTheDestroyer I need to write name of class to find out which class this method is from (not via IDE). I using QtCreator. Need it for docs. – bogdyname Jun 01 '20 at 11:46
  • Seriously, don't do this. No coder does this. F2 in QtCreater will "follow symbol". For help just press F1 on the method name. See https://doc.qt.io/qtcreator/creator-coding-navigating.html – GazTheDestroyer Jun 01 '20 at 11:50