2015-11-05 213 views
2

我想从特定位置打开一个文件,它似乎找到正确的路径,但我不明白为什么它总是跳过while循环。QT无法读取文件

QString utm_file_loc = "C:\\Example\\test\\UTM_Zone.config"; 
QFile fileutm(utm_file_loc); 
QTextStream utm_in(&fileutm); 
QString value; 
while(!utm_in.atEnd()) 
{ 
    QString line = utm_in.readLine();    
    line.replace(" ", ""); 
    if((line.indexOf("#") <0 || 1 < line.indexOf("#")) && 
     (line.contains("UTM_ZONE="))) 
    { 
     value = line.mid(line.indexOf("=")+1); 
     break; 
    } 
} 

配置文件是1号线和包含UTM_ZONE = 17

我想这可能与它是1号线,所以它始终认为这是在做底,但我想增加更多的线之前和之后的文件,它仍然跳过循环。

回答

3

,你做的文件对象的行,在那里你将它传递到QTextStream,你需要打开该文件的行之间:

if (fileutm.open(QIODevice::ReadOnly)) 
{ 
    //Create you QTextStream and use it here... 
} 
else 
{ 
    //Report error opening file here.... 
} 
+0

哇,是的,我只注意到这一点,刚要回答这个问题我自己。谢谢! – mchoy25