Reputation: 61
I iterate over a QByteArray which contains words. I will compare the array content with a given word (QString).
for(QByteArray::iterator it = content.begin(); it != content.end(); it++){
if(content.at(*it) == word){
...
}
}
the compiler say on line (if(content.at ..)
) : invalid conversion from 'char' to 'const char*' [-fpermissive]
how can i compare the values in this case?
Chris
Upvotes: 2
Views: 3286
Reputation: 6757
I iterate over an qbytearray which contains words from a file. I will compare each word with a given word.
Thanks for the clarification. In that case1 I would convert the QByteArray
to QString
and then split it into individual words which can then be trivially compared.
// QByteArray is implicitly convertible to QString
QString allWords = yourByteArray;
// split the string at each whitspace or newline
QStringList aWordlist = allWords.split(QRegExp("[\s\r\n]"), QString::SkipEmptyParts)
for (QStringList::iterator it=aWordlist.begin(); it != aWordlist.end(); ++it)
{
// it points to the next word in the list
if (*it == word)
{
...
}
}
1I'm assuming that you can't change the fact that you receive the file contents as byte array. Otherwise, it would probably be better to open a QFile
and read the contents from there.
How can i compare the values in this case?
According to the QString documentation, QString
can be compared to QByteArray
without iterating. So you could simply say:
QString word("Hello");
QByteArray bytes("hi");
if (word == bytes)
{
...
}
Upvotes: 1
Reputation: 61
I solved the problem: (QString word;
)
void MainWindow::startSearching()
{
word = ui->passwordTxt->toPlainText();
string a;
fstream inputFile;
inputFile.open(fileName.data());
while(!inputFile.eof()){
inputFile >> a;
if(a == word.toStdString()){
//anything
break;
}
}
inputFile.close();
}
Upvotes: 0
Reputation: 121
I think I know what you want to do and the problem is not so much with the comparison as with the fact that you have stored your text in a QByteArray
rather than a QString
, or some kind of container such as a QVector
, etc.
You need to look into different ways of reading in data from the QFile
class. Check out the docs here:
Upvotes: 0
Reputation: 71019
*it
is a byte and you try to compare it to a word(i.e. a sequence of chars). I am not sure what you are trying to do but maybe you should compare content.at(*it)
to the first char in the word?
Upvotes: 0
Reputation: 11058
QByteArray
contains bytes. QString
contains a string, that is a sequence of characters. A single byte cannot be compared to a sequence of bytes.
Upvotes: 1