Unsolved Get specific point from a .txt file
-
Imagine that a .txt file is like this:
...
Line 8: 1 - 2 yes 0.002452
Line 9: 1 - 3 no 0.015837
Line 10: 1 - 4 yeah 0.026817
...And I want to take the number 0.015837 in line 9. Is there something like get(9,5)?
get(9,5) = "0.015837"
get(10,2) = "-"
get(8,1) = "1"
get(9,4) = "no" -
Hi
Not really :)
a text files is a stream of bytes so its hard to
navigate to a line by random access as you dont really know
where lines starts or end unless u read and keep track.Can I ask what this file is ?
Is it too big to read into memory ?
-
Hi
You can read in the file and look at each line this wayQFile file("c:/myfolder/myfile.txt"); if(!file.open(QIODevice::ReadOnly)) { QMessageBox::information(0, "error", file.errorString()); } QTextStream in(&file); // format is: //1 - 2 yes 0.002452 while(!in.atEnd()) { QString line = in.readLine(); // read one line QStringList fields = line.split(" "); // split it to a list at all spaces // fields[0] is 1 // fields[2] is - // fields[3] is 2 // fields[4] is yes // fields[5] is 0.002452 } file.close();
-
It is a file that is generated after a calibration.
See the image, from notepad++: http://s3.postimg.org/6b35e23er/calibration.png
But why I don't really know where the lines start or end? It is not so difficult to be seen by human eye.
All .txt files have lines, and you can distinguish the elements of any line, just by a space between them.
So, it is really so difficult to get the 3rd element of the 8th line of a .txt file?
edit: @mrjj, thanks, but I think in your case, fields[1] is blank? And I can take the number 0.002452 directly or step by step and digit by digit?
-
well its not how files works. :)
But see code, its not so difficult to read in.
If all have spaces U can easy split into a list. -
@Konstantinos said:
but I think in your case, fields[1] is blank? And I can take the number 0.002452 directly or step by step and digit by digit?Whoops. its a typo. there be [1] also
// fields[0] is 1
// fields[1] is -
// fields[2] is 2
// fields[3] is yes
// fields[4] is 0.002452
It will split at space.You will get "0.002452" from the list[4]
(full number)EDIT:
and u should skip first line. -
Ok thanks @mrjj. I wanted exactly this...
-
@Konstantinos
Super :)
For a little error check, u can check fields.size()
and see if it has expected number of entries.
That way u can skip lines if not valid. -
But why I don't really know where the lines start or end? It is not so difficult to be seen by human eye.
All .txt files have lines, and you can distinguish the elements of any line, just by a space between them.Three reasons:
- Line endings. The special characters that mark where a line ends are different for different platforms.
- Linux uses the line feed character "\n"
- Windows uses carriage return followed by line feed "\r\n"
- And finally Mac uses carriage return only "\r"
- Text encoding. Characters are represented as numbers so a given number would correspond to a given character, hence you can see them on the screen. Now, consider the amount of languages and characters that people had invented during the ages ... So the encoding specifies which character is represented by what number, but because just having one big table for all languages is pretty inefficient there are the encoding schemes that use variable length to represent a character (like UTF-8, one of the most prominent examples). This means that a character may be represented by 1, 2, 3 or even 4 consecutive 1 byte numbers, so you can't tell before actually reading the file.
- Line length. You can't know how long a line of text is before actually reading it.
Kind regards.
- Line endings. The special characters that mark where a line ends are different for different platforms.