質問

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 ?

役に立ちましたか?

解決

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;

他のヒント

static_cast doesn't work that way, so instead of a static_cast, you should use QString::number to convert an int into a QString.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top