Results 1 to 9 of 9

Thread: How to read data from a particular serial numer line from a text file

  1. #1
    Join Date
    Jul 2009
    Location
    Bangalore
    Posts
    68
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default How to read data from a particular serial numer line from a text file

    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
    Thanks & Regards
    Sandeep G.R.

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to read data from a particular serial numer line from a text file

    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.

  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to read data from a particular serial numer line from a text file

    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?

  4. #4
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read data from a particular serial numer line from a text file

    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 ?

  5. #5
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to read data from a particular serial numer line from a text file

    Quote Originally Posted by Le_B View Post
    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:
    Qt Code:
    1. //abstract
    2. int structure_index = 630;
    3. headerFile.seek( structure_index * sizeof( int ) );
    4.  
    5. int structure_position = 0;
    6. headerFile >> strcuture_position;
    7.  
    8. dataFile.seek( structure_position );
    9. dataFile >> structure;
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to read data from a particular serial numer line from a text file

    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

  7. #7
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: How to read data from a particular serial numer line from a text file

    Quote Originally Posted by Spitfire View Post
    Qt Code:
    1. //abstract
    2. int structure_index = 630;
    3. headerFile.seek( structure_index * sizeof( int ) );
    4.  
    5. int structure_position = 0;
    6. headerFile >> strcuture_position;
    7.  
    8. dataFile.seek( structure_position );
    9. dataFile >> structure;
    To copy to clipboard, switch view to plain text mode 
    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.

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to read data from a particular serial numer line from a text file

    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.

  9. #9
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to read data from a particular serial numer line from a text file

    Quote Originally Posted by Atomic_Sheep View Post
    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

Similar Threads

  1. QFile can't read a file
    By Raccoon29 in forum Qt Programming
    Replies: 3
    Last Post: 11th February 2009, 20:24
  2. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Replies: 3
    Last Post: 23rd June 2006, 17:46
  5. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.