• 4620阅读
  • 3回复

一个控制台程序,请大家帮忙看一下输出结果,不理解! [复制链接]

上一主题 下一主题
离线sunvim
 

只看楼主 倒序阅读 楼主  发表于: 2009-10-09
#include <QtCore\QFile>
#include <iostream>
#include <QtCore\QIODevice>
using   namespace    std;
int main(int argc,char **argv)
{
    char line[80];
    cout<<"Hello, world!"<<endl;
    QFile qfile("rwfile.txt");
    if(qfile.open( QIODevice::ReadOnly)) {
        while(!qfile.atEnd()) {
            if(qfile.readLine(line,strlen(line)) > 0)
                cout << line;
        }
        qfile.close();
    }
    if ( !qfile.open(QIODevice::ReadOnly))
    {
        cout<<" ERROR: CAN'T OPEN TIS FILE!"<<endl;
    }

    return  0;
}


本段程序,为何输出是:

QIODevice:: readline: called  with maxSize>2


rwfile.txt 文本内容是:
The first line
The second line and a bit more
Multiplying 34 by 86 gives 2924
[ 此帖被sunvim在2009-10-09 10:42重新编辑 ]
离线lugaideath

只看该作者 1楼 发表于: 2009-10-09
晕!这样写对么?
你打开了两次文档,第一次个if打开就读文档,第二个if再打开一次

你第二个if改成else
  else
    {
        cout<<" ERROR: CAN'T OPEN TIS FILE!"<<endl;
    }

其他问题自己慢慢调试看看吧
离线foxyz

只看该作者 2楼 发表于: 2009-10-09
qfile.readLine(line,strlen(line))
改成qfile.readLine(line,sizeof(line)) ;
别open两遍,有没有open成功的判断前边都有了。

qt的例子:
QFile file("box.txt");
if (file.open(QFile::ReadOnly)) {
     char buf[1024];
     qint64 lineLength = file.readLine(buf, sizeof(buf));
     if (lineLength != -1) {
         // the line is available in buf
     }
}
离线sunvim

只看该作者 3楼 发表于: 2009-10-09
#include <QtCore\QFile>
#include <iostream>
#include <QtCore\QIODevice>
using   namespace    std;
int main(int argc,char **argv)
{
   // char line[80];
     qint64 lineLength;
    cout<<"Hello, world!"<<endl;
    QFile qfile("rwfile.txt");
    if(qfile.open( QIODevice::ReadOnly)) {
       char buf[1024];
       while ( !qfile.atEnd())
       {
         lineLength = qfile.readLine(buf, sizeof(buf));
     if (lineLength != -1) {
         cout<<buf<<endl;
     }
       }

    qfile.close();

    }
    else
    {
        cout<<" ERROR: CAN'T OPEN TIS FILE!"<<endl;
    }

    return  0;

这是完整版,非常感谢楼上提醒!
}
快速回复
限100 字节
 
上一个 下一个