这两天在读取一个文件,发现需要将其数据分开,我的数据源是每行一个对象, 其中只有数据,空格。所以我就想按行读取,并且跳过空格。最终实现数据的加载。
421.519 -23.3112345 81230
-31.9861 -61.812332 8130
-11.12365 -5.70123 8130
-61.123 -42.6123 80
-50.12321 -3.71239 8340
40.713123 -2.912379 15280
31.71233 -23.12365 82340
22.812373 -1.712326 85420
32.991236 -1.3123 23423
15.12368 -03.921399 1542
61.321315 -0.611231 4234
73.1237 -03.35123 80
82.72131241 -0.151233 24234
91.9123 -03.01238 23423
这是我的数据,每行三个数,需要对其进行操作。
所以我实现的代码是:
//读取文件
QFile file("D:/Z76.2");//与文件建立联系
if(file.open(QIODevice::ReadOnly))//打开文件,以只读的方式打开文本文件
{ //还有很多方式,大家可以看一下
QFile file("D:/Z76.2");
if (file.open(QIODevice::ReadOnly))
{
while (!file.atEnd())
{
QByteArray line = file.readLine();
QString x = "";
for(int i= 0 ;i<line.length();i++)
{
if(isspace(line[i]))//判断是否是空格,空格直接跳过
{
displayString.append(x);
x="";
continue;
}else
{
x.append(line[i].operator char());//继续添加
}
}
}
file.close();
}
}