0

Using Qt 4.7, I need to look for a file in a given directory that has a certain name. If it is found, I need to get the text data from within that file. I have the code set up as follows:

    QDirIterator iterator(dir_name, QDirIterator::IteratorFlag);  
    while(iterator.hasNext()  
    { 
        if(iterator.fileName() == nameOfNeededFile)
        {
            //Code need here to get data!
        }
    }

It's also probably worth noting that the directory only contains files, no subdirectories.

thnkwthprtls
  • 3,287
  • 11
  • 42
  • 63
  • Why don't just open the file you need? There is no need to "search" the file using iterators. You probably need to read the documentation of [QFile](http://qt-project.org/doc/qt-5.0/qtcore/qfile.html). –  Aug 22 '13 at 14:38
  • 2
    I can't see any problem in your file reading code other than the fact that it is nonexistent. What have you tried and what didn't work? – erelender Aug 22 '13 at 14:48
  • `QDirIterator::IteratorFlag` is a type, not a value [[link](http://qt-project.org/doc/qt-5.1/qtcore/qdiriterator.html#IteratorFlag-enum)]. This code won't even compile. – cgmb Aug 22 '13 at 17:25

1 Answers1

0

as being mentioned in comments you don't need an iterator..

QByteArray data;

if (QFile::exists("<your file name>")) {
   QFile f("your file");
   if (f.open( QIODevice::ReadOnly )) {
      data = f.readAll();
      f.close();
   }
}
evilruff
  • 3,947
  • 1
  • 15
  • 27