Results 1 to 9 of 9

Thread: How to put each word of a string into an array

  1. #1
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default How to put each word of a string into an array

    Hello every one i am stuck at a small problem. I have a text file with some data which i am reading from my Qt program. I put the read text string into a QString container. I am trying to split the string after every blank space and want to put it in a array
    and access any particular data position with its index number.
    Qt Code:
    1. QFile file("C:/thermaldata.txt");
    2. QString line;
    3. if ( file.open(IO_ReadOnly | IO_Translate))
    4. {
    5. QTextStream t( &file );
    6. QTextStream stream( &file );
    7. line.append(stream.readLine());
    8. file.close();
    9. }
    10. list = line.split(" "); // trying to split the string after every blank space
    11. }
    To copy to clipboard, switch view to plain text mode 

    I wanted to put each word in the text string in an array something like
    list.at(0) should have 5/3/2011
    list.at(1) should have 11:47:24
    so that i can access any word according to their position.
    This is the text string i read from the text file.

    Qt Code:
    1. ("5/3/2011 11:47:24 AM 1.33E+3 853 1.33E+3 868 P235.529 235.864 27.014 26.505 27.108 26.695 26.806 26.602 26.852 236.065 30.987 27.160 27.969 27.222 26.199 30.690 33.263 26.576 26.518 236.360 236.626 26.590")
    To copy to clipboard, switch view to plain text mode 


    how should i go about doing this.. ?? i tried a few things but couldn't get what i wanted.

    Thank you

  2. #2
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to put each word of a string into an array

    You have a QTextStream so use operator >>:
    Qt Code:
    1. QFile file("C:/thermaldata.txt");
    2. QString item;
    3. if ( file.open(IO_ReadOnly | IO_Translate)) {
    4. QTextStream t( &file );
    5.  
    6. while(t.atEnd()) {
    7. t >> item;
    8. porcess(item);
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    see http://doc.qt.nokia.com/latest/qtext...erator-gt-gt-4

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to put each word of a string into an array

    And this line list = line.split(" "); doesn't work? Be more specific about what happens, else we can't really help you.

  4. The following user says thank you to Zlatomir for this useful post:

    nagabathula (11th May 2011)

  5. #4
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to put each word of a string into an array

    He has not only spaces there (probably horizontal tab too).
    PS. you can also use regular expretion (not only spaces will be treated as separators):
    Qt Code:
    1. line.split(QRegExp("\\s+"));
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to MarekR22 for this useful post:

    nagabathula (11th May 2011)

  7. #5
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: How to put each word of a string into an array

    Hello MarekR thanks for the reply.. i have been trying to implement what you suggested. But not getting it right i read the detailed description but couldn't figure out how i should use this to put each word of the sentence in a array one after another. This is my first time using QTextStream

    Qt Code:
    1. QTextStream & QTextStream::operator>> ( QString & str )
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QFile file("C:/thermaldata.txt");
    2. QString item;
    3. if ( file.open(IO_ReadOnly | IO_Translate)) {
    4. QTextStream t( &file );
    5.  
    6. while(t.atEnd()) {
    7. t >> item;
    8. //porcess(item);
    9. }
    10. qDebug()<<item;
    11. }
    To copy to clipboard, switch view to plain text mode 
    i checked in qDebug()<<item; but the string is not present in the item container. not able to figure out how i should use this
    QTextStream:perator>> ( QString & str )
    it looks perfect for my program, it separates each word by the space and put it in the a string i could access each word by using its position but it is not showing the text string at all. ?

    thank you

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

    Default Re: How to put each word of a string into an array

    QString.split() does exactly what you want. Why is the result of using it not acceptable?

  9. The following user says thank you to SixDegrees for this useful post:

    nagabathula (11th May 2011)

  10. #7
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re: How to put each word of a string into an array

    hi i tried QString.split() but when i try to access a word from the text string which is in the QString line. for example i try to access the third word line.at(3) i see a / instead of the third word which is 1.33E+3 it is taking the 3rd character from the string. where in i wanted to access the third word.

    Qt Code:
    1. "5/3/2011 11:47:24 AM 1.33E+3 853 1.33E+3 868 P235.529 235.864 27.014 26.505 27.108 26.695 26.806 26.602 26.852 236.065 30.987 27.160 27.969 27.222 26.199 30.690 33.263 26.576 26.518 236.360 236.626 26.590"
    To copy to clipboard, switch view to plain text mode 

    thank you

  11. #8
    Join Date
    Nov 2010
    Posts
    100
    Thanks
    38
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4

    Default Re:Creating a .dat extension file.

    Hi SixDegrees that made me look back into qt assistant and the example was there i had to put the split string into a StringList which i was not doing. I can access any word with the index position.. Thanks everyone for helping me out. ..

    Regards


    Added after 14 minutes:


    Hello i have another doubt off this topic.. wanted to know how i can create a .dat file from my program. Do i have to jus specify the extension .dat to the file to create , right now i am creating a txt file with some data in it. i wanted the same to be in .dat format.

    Thank you
    Last edited by nagabathula; 11th May 2011 at 16:12.

  12. #9
    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: How to put each word of a string into an array

    You can call your file whatever your operating system will allow and write anything you like into it. Want to write a file called "something.dat" filled with English text, Swahili text, or binary image data, go right ahead. ".dat" is not a description of a file format, it is part of a file name.
    Last edited by ChrisW67; 12th May 2011 at 00:52.

Similar Threads

  1. Replies: 3
    Last Post: 21st October 2012, 12:31
  2. Replies: 3
    Last Post: 5th February 2011, 07:31
  3. how to get the word when i highlight a word with mouse?
    By saleh.hi.62 in forum Qt Programming
    Replies: 0
    Last Post: 4th July 2010, 08:24
  4. Conversion Char Array to string
    By anafor2004 in forum Newbie
    Replies: 6
    Last Post: 6th May 2008, 14:35
  5. String Array
    By Sarma in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2006, 07:53

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.