Qt-如何计算.txt文件中的行数 [英] Qt - How to count number of line in .txt file

查看:3079
本文介绍了Qt-如何计算.txt文件中的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算文本文件中的行数,因此我可以将其转换为二维数组

I want to count number of line in text file, so i can convert it to two dimensional array

文本文件应该是这样的

20
30
78
1000
....
....

和使用QFile访问文件的源代码

and source code using QFile to access file

QFile file("c:/Qt/in.txt");
file.open(QIODevice::ReadOnly); //| QIODevice::Text)
y = linecount/5;
QString line[y][5];
QTextStream in(&file);
for (int k=0;k<=y;k++)
{
    for (int x=0;x<=4;x++)
    {
        line[i][x] = in.readLine();
    }
}

推荐答案

您的问题不清楚,代码中也有一些参数.要计算文件中的行数,它很简单,如下所示.您可以使用数组行填充二维数组.

Your Question is not clear, and also some parameters in your code. For counting number of lines in a file, it is as simple as given below. You can use array line to populate your 2-d array.

QFile file("c:/Qt/in.txt");
int line_count=0;
file.open(QIODevice::ReadOnly); //| QIODevice::Text)
QString line[100];
QTextStream in(&file);
while( !in.atEnd())
{
    line[line_count]=in.readLine();
    line_count++;
}

这篇关于Qt-如何计算.txt文件中的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆