Qt中怎样读取文件指定的行?

 我来答
chineseboytom
2012-03-27 · 超过15用户采纳过TA的回答
知道答主
回答量:45
采纳率:0%
帮助的人:32.2万
展开全部
你提到行,很明显你要操作的是文本文件,文本文件是顺序文件,只能用
QFile file("FileName");
QTextStream in(&file);
int i=0;
QString line="";
while (!in.atEnd() && ++i<=lineNo ){
line=in.readLine();
}
其它用途请用随机存储!
singolang
2012-03-27 · TA获得超过832个赞
知道小有建树答主
回答量:1505
采纳率:0%
帮助的人:758万
展开全部
1、qint64 QFile::readLineData ( char * data, qint64 maxlen )
[virtual protected]
Reimplemented from QIODevice::readLineData().

2、qint64 QIODevice::readLine ( char * data, qint64 maxSize )
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. If a line could not be read but no error ocurred, this function returns 0. If an error occurs, this function returns the length of what could be read, or -1 if nothing was read.
A terminating '\0' byte is always appended to data, so maxSize must be larger than 1.
Data is read until either of the following conditions are met:
The first '\n' character is read.
maxSize - 1 bytes are read.
The end of the device data is detected.
For example, the following code reads a line of characters from a file:
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
}
}

3、QByteArray QIODevice::readLine ( qint64 maxSize = 0 )
This is an overloaded function.
Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray.
This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
522583936a8ef7
2018-05-29
知道答主
回答量:3
采纳率:0%
帮助的人:3140
展开全部
QFile file(srcDir);
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
int row=0;
while (!file.atEnd())
{
QByteArray line = file.readLine();
if(++row==2){
//QString a=file.readLine();
QString str(line);
qDebug() << str;
}
}
file.close();

}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式