Qt中怎样读取文件指定的行?
答案:3 悬赏:10
解决时间 2021-01-22 21:10
- 提问者网友:愿为果
- 2021-01-22 04:03
Qt中怎样读取文件指定的行?
最佳答案
- 二级知识专家网友:洎扰庸人
- 2021-01-22 05:36
你提到行,很明显你要操作的是文本文件,文本文件是顺序文件,只能用
QFile file("FileName");
QTextStream in(&file);
int i=0;
QString line="";
while (!in.atEnd() && ++i<=lineNo ){
line=in.readLine();
}
其它用途请用随机存储!
QFile file("FileName");
QTextStream in(&file);
int i=0;
QString line="";
while (!in.atEnd() && ++i<=lineNo ){
line=in.readLine();
}
其它用途请用随机存储!
全部回答
- 1楼网友:何以畏孤独
- 2021-01-22 06:05
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();
}
- 2楼网友:逐風
- 2021-01-22 05:58
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.
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯