View Full Version : How to put each word of a string into an array
nagabathula
11th May 2011, 10:25
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.
QFile file("C:/thermaldata.txt");
QString line;
if ( file.open(IO_ReadOnly | IO_Translate))
{
QTextStream t( &file );
QTextStream stream( &file );
line.append(stream.readLine());
file.close();
}
QStringList list;
list = line.split(" "); // trying to split the string after every blank space
}
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.
("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")
how should i go about doing this.. ?? i tried a few things but couldn't get what i wanted.
Thank you
MarekR22
11th May 2011, 10:45
You have a QTextStream (http://doc.qt.nokia.com/latest/qtextstream.html) so use operator >>:
QFile file("C:/thermaldata.txt");
QString item;
if ( file.open(IO_ReadOnly | IO_Translate)) {
QTextStream t( &file );
while(t.atEnd()) {
t >> item;
porcess(item);
}
}
see http://doc.qt.nokia.com/latest/qtextstream.html#operator-gt-gt-4
Zlatomir
11th May 2011, 10:46
And this line list = line.split(" "); doesn't work? Be more specific about what happens, else we can't really help you.
MarekR22
11th May 2011, 10:50
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):
line.split(QRegExp("\\s+"));
nagabathula
11th May 2011, 12:16
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
QTextStream & QTextStream::operator>> ( QString & str )
QFile file("C:/thermaldata.txt");
QString item;
if ( file.open(IO_ReadOnly | IO_Translate)) {
QTextStream t( &file );
while(t.atEnd()) {
t >> item;
//porcess(item);
}
qDebug()<<item;
}
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::operator>> ( 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
SixDegrees
11th May 2011, 12:23
QString.split() does exactly what you want. Why is the result of using it not acceptable?
nagabathula
11th May 2011, 12:30
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.
"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"
thank you
nagabathula
11th May 2011, 16:12
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
ChrisW67
12th May 2011, 00:47
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.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.