PDA

View Full Version : How to read data from a particular serial numer line from a text file



grsandeep85
23rd July 2009, 11:06
Hi All,

I have a text file in the format
#n X Y Z
0 123 456 789
1 321 654 987
2 345 243 809
......
.....
...... and so on

Now i need to input the serial number (say 2) and corresponding X Y Z values should be read. how to do this please help us it would be great pleasure :)

nish
23rd July 2009, 12:11
because the lines are of variable length.. you cannont jump to a perticular line... you have to read the text file line by line...

if you control the writing of this file as welll then,,, it would be better if you convert this file into binary.. using a simple struct per line... this way you can anytime seek to a perticular record.

Atomic_Sheep
2nd May 2012, 14:02
Sorry to bring up an old thread and stealing the thread, but I was wondering whether you would suggest creating 1 struct per row with members being the columns or how would you go about it?

Le_B
2nd May 2012, 14:25
if i understand the problem correctly :

you enter n and you want xyz
so it is something like:
QTextStream::readLine().split(" ");
and you got a list with x y z at 1 2 3
no ?

Spitfire
3rd May 2012, 13:13
if i understand the problem correctly :
Then you don't undestand the problem correctly :) the goal is not to read the whole content to get to n'th line. If you want last entry in the file you would need to read whole file using readLIne().


How you design your structure is up to you and depends on the data you have.
For the example given at the beggining simple structure with 4 ints is sufficient.

The most important part is to be able to tell where in the file your data is. If you want to get n'th record you need to be able to get pos = n*sizeof( struct ).
That implies that structure size can't be variable (no strings, vectors etc).

If you need variable size members then structure should contain member describing its size so you can jump around a file reading only size of the structure to be able to jump to the end of it and read another one, and so on and so forth.

If your files are huge and structures are variable length, then you may consider spliting it into two files, header and data blob. where data contains only data, header would be an array telling you where in the blob certan structure is:


//abstract
int structure_index = 630;
headerFile.seek( structure_index * sizeof( int ) );

int structure_position = 0;
headerFile >> strcuture_position;

dataFile.seek( structure_position );
dataFile >> structure;

Le_B
3rd May 2012, 13:41
ok :)


I have a text file in the format
#n X Y Z
0 123 456 789
1 321 654 987
2 345 243 809

from that i thought the file format was already decided and couldn't be changed
sorry

Atomic_Sheep
3rd May 2012, 16:57
//abstract
int structure_index = 630;
headerFile.seek( structure_index * sizeof( int ) );

int structure_position = 0;
headerFile >> strcuture_position;

dataFile.seek( structure_position );
dataFile >> structure;


If I'm reading this correctly, this example can only deal with data of the same size i.e. int? So once again, you can't mix and match strings with ints in this type of data set?

I may not fully understand exactly what that code does (I'll figure it out eventually after a couple of reviews) but I'm pretty sure I know what you're trying to get at and it's given me a few ideas so thanks very much.

yeye_olive
4th May 2012, 13:48
I agree with the opinion of switching to a binary format with a constant record size. However the original text file format under consideration has an interesting feature: the line number is actually written at the beginning of the line. It means that one does not have to read the file sequentially, but may instead seek to any point the file, read past the next newline character and read the next line number. One may then e.g. perform a binary search, or initially seek to a position based on the average length of a line, etc. This would be an interesting (read: challenging and involving some debugging) exercise.

Spitfire
8th May 2012, 10:39
If I'm reading this correctly, this example can only deal with data of the same size i.e. int? So once again, you can't mix and match strings with ints in this type of data set?

Not at all. Your structure can consist of what you want.
Imagine a structure that has 1 int and 2 strings (or any other variation). Event if each string is different size in each structure, the size of the structure will be described by single int value ( sizeof( struct ) ).
Your header is just a bunch of that int values describing position of structure in the blob. You would have as many ints in the header as structures you have in the blob file.

I hope this makes it little clearer what I meant :)