Click here to Skip to main content
15,069,020 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
and read the next line. Any help would be appreciated

NOTE:: Lines in .txt file are not same in size
Example
C#
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 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;
			             
              }
          }

	}


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 ???
Posted
Updated 21-Jun-16 4:01am
v4

See : getline (string) - C++ Reference[^]
and count the lines until you get where you want (since you have variable length lines).
   
Comments
XamBEE 21-Jun-16 7:39am
   
BUT i want to avoid to read lines (n-1) to goto line n
Mehdi Gholam 21-Jun-16 9:18am
   
You could create a line pointer index, but that is the same as reading all the lines in the first place (although if you intend to read the same file multiple times then it would make sense).
XamBEE 21-Jun-16 10:03am
   
I just added the sample function of reading file. would be nice if you can tell me how to add line pointer.
Mehdi Gholam 21-Jun-16 10:06am
   
Read each line and store the file pointer offset in an array, save that array for later.
Sergey Alexandrovich Kryukov 21-Jun-16 11:55am
   
5ed.
—SA
Mehdi Gholam 21-Jun-16 12:34pm
   
Cheers Sergey!
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).

or
  • Read the file in big chunks and skip the unwanted lines in memory.
   
Comments
XamBEE 21-Jun-16 8:23am
   
Can you explain first options a bit. I dont know exactly how?
CPallini 21-Jun-16 8:44am
   
It is what Mehdi Gholam already suggested. I know you don't like it, but there are no alternatives (other than finding yourself the newlines, as in option 2) because the file lines haven't the same length.
Sergey Alexandrovich Kryukov 21-Jun-16 11:55am
   
5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900