逐行读取txt文件 [英] Read txt file line by line

查看:117
本文介绍了逐行读取txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想逐行读取文件(.txt)但不要在while循环中读取(一旦读取整个文件)。目前我正在整理文件一次。但是我想要做的是读取一行而不是回到正常的MAIN函数,而不是稍后调用

I want to read the file(.txt) line by line but not in while loop(once reading whole fle). Currently I am reading the file once as whole. BUT what I want to do is to read one line than go back to normal MAIN function than later on call

ReadDumpFile

并读取下一行。任何帮助将不胜感激



注意:: .txt文件中的行大小不同

示例

and read the next line. Any help would be appreciated

NOTE:: Lines in .txt file are not same in size
Example

5:50:12:560:434,    Rx, 7,  0x1E3,       00 80 2B 00 77 00 7D
5:50:12:560:615,    Ax, 4,  0x760,       00 12 84 41
5:50:12:560:826,    gc, 5,  0x2f1,       64 5A 43 20 4E







READ文件功能




READ File function

readingLogFile(){
	
	QString LFileName = "P_1.trc";
    QFile logfile(LFileName );
      
      QTextStream in(&logfile);
      while (!in.atEnd())
      {
          QString fileLine = in.readLine(); //
          if(fileLine.trimmed().isEmpty()!=true)
          {
            qDebug()<<fileLine;
			             
              }
          }

	}





什么我试过了:



我认为我需要做的是将前一行保留在指针中。并且净时间先前为+ 1。但不知道如何阅读文档中的特定行。如果.txt文件包含1000000行。我怎么读行#670000 ???



What I have tried:

What i think i need to do is to keep previous line in pointer. And net time previous+1. but dont know how to read a specific line inside the document. if .txt file contain 1000000 line. how i can read line#670000 ???

推荐答案

参见: getline(string) - C ++参考 [ ^ ]

并计算直到你想要的线(因为你有可变长度的线)。
See : getline (string) - C++ Reference[^]
and count the lines until you get where you want (since you have variable length lines).


由于行该文件有不同的长度,我只看到两个选项,你要么

  • 逐行读取文件(即多次调用getline)。
Since the lines of the file have different lengths, I see just two options, you either
  • Read the file line by line (that is call multiple times getline).
  • 以大块的形式读取文件并跳过内存中不需要的行。


这篇关于逐行读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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