Results 1 to 6 of 6

Thread: Reading delimited blocks in a txt file and puting it in a list of strings

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Reading delimited blocks in a txt file and puting it in a list of strings

    Hello!

    I have the need of creating a software that is capable of reading lines from a text (let us say, from a .txt file) in pre-programmed blocks and saving them in a list of strings.

    So, for example, here is the first line of the cput-stat file of my PC:


    cpu 345652 7067 100651 5176702 20949 2797 1185 0 0


    Imagine that I'm trying to read this first line (which is what I'm actually trying, for tests). But I don't want to read the entire line directly; i want to read just the "cpu" part. In this case, I would use the Qt function "readLine(3)" as a way of doing it

    But what if I want to read only the second column? [345652] How can I say in a function that I want to read from bit 6 to 12 only and save it in a string without the rest of the line?

    If I'm not confusing things, when I began learning C++ in the university my professor show me a function, in C++, that one could use to read a line and stop reading when a empty space appear. If that is correct, than I could use a series of this function in order to read all of the column, save thoose that I want and discard the undesireble ones.

    The problem is: I don't know anymore the code for that function.


    So my question is: how can I read, ether with C++ or Qt codes, delimited blocks of data in a line of text?


    I would be glad if someone could give me a code example for that.


    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading delimited blocks in a txt file and puting it in a list of strings

    IMHO you can:

    1. Read the entire line with QIODevice::readLine
    2. split the line in tokens with QString::split
    3. read the token you need QList::at
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Reading delimited blocks in a txt file and puting it in a list of strings

    This:
    than I could use a series of this function in order to read all of the column, save thoose that I want and discard the undesireble ones.
    is conflicting with
    But I don't want to read the entire line directly; i want to read just the "cpu" part. In this case, I would use the Qt function "readLine(3)" as a way of doing it
    So either its ok for you to read the whole line, parse it, and discard what you don't need , or its not ok.
    You have to decide.
    If I'm not confusing things, when I began learning C++ in the university my professor show me a function, in C++, that one could use to read a line and stop reading when a empty space appear.
    Various libs implement readline() function, which usually reads until the first \r\n, including Qt (see QDevice or QFile, QTextStream)

    i want to read just the "cpu" part. In this case, I would use the Qt function "readLine(3)" as a way of doing it
    This prerequisites that you know the correct starting points of your columns.
    If this is the case you can jump directly to a start of a desired "column" by using seek().
    Note, you will have to bookkeep your position in the file, as the file is serial, and "columns" are only a logical interpretation done by you.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Reading delimited blocks in a txt file and puting it in a list of strings

    There a many ways to achieve this. Here is one that uses QTextStream to worry about finding the white space delimiter:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3.  
    4. int main(int argc, char* argv[])
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QFile in("/tmp/data.txt");
    9. if (in.open(QIODevice::ReadOnly)) {
    10. QTextStream s(&in);
    11. while (!s.atEnd()) {
    12. QString dummy, value;
    13. s >> dummy; // ignore the first field
    14. s >> value;
    15. dummy = s.readLine(); // ignore the remainder of the line
    16. qDebug() << value;
    17. }
    18. in.close();
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    No matter what approach you take you have to read the entire line in order to get to the next line.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Reading delimited blocks in a txt file and puting it in a list of strings

    The STL function you're thinking of is istream::getline. You use the second argument to change the delimiter from the default newline to whatever separator you want, in this case a space.

    To allow for cases where there are multiple spaces between words, you'll also need a function to strip leading/trailing space off the resulting strings. The std::string class provides everything you need to implement this.

  6. #6
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Reading delimited blocks in a txt file and puting it in a list of strings

    Quote Originally Posted by high_flyer View Post
    This:

    is conflicting with


    So either its ok for you to read the whole line, parse it, and discard what you don't need , or its not ok.
    You have to decide.
    Hello, hygh_flyer!

    First, thanks for the answer. But second, I would like to notice that there is no conflict between the two sentences, since the first one describes the type of function I want (one that can read delimited parts of a line directly or else read part by part, save those that I want and discard those that I don't) and the second, an example =]

    But anyway, thanks!

    Hello everyone!

    I would like to thank you for all of your answers. I will try one by one to see what is the best and I hope that till midday I'll come back here to close the topic saying that the problem is solved

    Thank you once again!

    God bless!

Similar Threads

  1. Replies: 4
    Last Post: 9th August 2012, 21:08
  2. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 18:55
  3. Replies: 3
    Last Post: 20th April 2011, 22:23
  4. SQLite reading Chinese strings issue
    By yazwas in forum Qt Programming
    Replies: 1
    Last Post: 6th April 2011, 08:37
  5. Read delimited Lines in a file
    By mourad in forum Newbie
    Replies: 1
    Last Post: 19th April 2008, 09:18

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.