Yui
Yui

Reputation: 69

multiple file reader in qt c++

I want to specify a path to a folder and want to open all xslt files in said folder one after another.

I tried to do it like this:

 QString path = "/misc/example";
 QDir dir(path);
 QStringList filters;
 filters << "*.xslt";

 foreach (QFile file, dir.entryList(filters, QDir::Files))
 {
      if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
      {
        qDebug("Failed to open file for reading.");
        return 0;
      }
 }

but i get this error

/usr/include/qt4/QtCore/qfile.h:209: Fehler:'QFile::QFile(const QFile&)' is private

what am i doing wrong?

Upvotes: 0

Views: 2152

Answers (2)

borisbn
borisbn

Reputation: 5054

entryList returns QStringList but not some container of QFiles. So you should act like this

...
foreach ( QString fileName, dir.entryList(filters, QDir::Files) )
{
    QFile inFile( fileName );
    if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        qDebug("Failed to open file for reading.");
        return 0;
    }
}

To avoid big overhead with foreach and making a list of filenames, I suggest to use QDirIterator

QDirIterator dirIt( path, filters, QDirIterator::Subdirectories );
while ( it.hasNext() )
{
    QFile inFile( it.next() );
    if( !inFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
    {
        qDebug("Failed to open file for reading.");
        return 0;
    }
}

Upvotes: 2

masoud
masoud

Reputation: 56519

About the error, this line

 foreach (QFile file, dir.entryList(filters, QDir::Files))

tries to to copy an object from dir.entryList as a QFile to file while copy-constructor is private. Then, it shows the error.

Try something like this:

foreach (QString &name, dir.entryList(filters, QDir::Files))
{
    QFile file(name);
    if (!file.open (QIODevice::ReadOnly | QIODevice::Text))
    {
      // ...
    }
}

Upvotes: 2

Related Questions