Search code examples
c++qtqfile

QFile is not reading nor opening my file


I have a file called "sequence_30.dat" that contains a sequence of 1 and -1 in a vertical representation (i.e.: each 1 or -1 is in a separate line) .. I am trying to read the file for another operation using the following code:

int length = 31
QFile file("sequence_"+ (static_cast<QString>(length)) +".dat");
if(file.exists()){
  file.open(QIODevice::ReadOnly);
  if(file.isOpen()){
    ....
    ....
  }
  file.close();
}

but when debugging, the compiler skips the "if(file.exists())" statement and when it is removed the compiler again skips the "if(file.isOpen())" statement

I am very sure that path is correct, but if is not how to make sure that I am in the right path (i.e.: is there is a way to check where am I reading from) .. and if the path is correct why my file is not opening ?


Solution

  • static_cast<QString>(length)
    

    Should be:

    QString::number( length )
    

    You can check it by just printing it out to the console:

    cout << qPrintable( QString( "sequence_" ) +
            QString::number( length ) + ".dat" ) << endl;