Qt中怎样读取文件指定的行?
3个回答
展开全部
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.
[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.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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();
}
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();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询