0
votes

How can i find a specific character in a QFile which has a text in it?

for example i have ' $5000 ' written somewhere in my file. in want to find the "$" sign so i will realize that I've reached the number.

I tried using QString QTextStream::read(qint64 maxlen) by putting 1 as the maxlen :

QFile myfile("myfile.txt");
myfile.open(QIODevice::ReadWrite | QIODevice::Text);

QTextStream myfile_stream(&myfile);

  while(! myfile_stream.atEnd())
          {
              if(   myfile_stream.read(1) == '$')
               {

                  qDebug()<<"found";
                  break;
              }
          }

and i get "error: invalid conversion from 'char' to 'const char* "

i also tried using the operator[] but apparently it can't be used for files.

5

5 Answers

1
votes

Read in a line at a time and search the text that you've read in

QTextStream stream(&myFile);
QString line;
do 
{
    line = stream.readLine();
    if(line.contains("$"))
    {
        qDebug()<<"found";
        break;
    }
} while (!line.isNull());
0
votes

The error message you've posted doesn't match the issue in your code. Possibly the error was caused by something else.

QTextStream::read returns QString. You can't compare QString and const char* directly, but operator[] can help:

QString s = stream.read(1);
if (s.count() == 1) {
  if (s[0] == '$') {
    //...
  }
}

However reading a file by too small pieces will be very slow. If your file is small enough, you can read it all at once:

QString s = stream.readAll();
int index = s.indexOf('$');

If your file is large, it's better to read file by small chunks (1024 bytes for example) and calculate the index of found character using indexOf result and count of already read chunks.

0
votes

a single char could be read with

QTextStream myfile_stream(&myfile);
QChar c;
while (!myfile_stream.atEnd())
  myfile_stream >> c;
  if (c == '$') {
    ...
  }
0
votes

myfile_stream.read(1) - this is not good practice, you should not read from file one byte at a time. Either read the entire file, or buffered/line by line if there is a risk for the file to be too big to fit in memory.

The error you get is because you compare a QString for equality with a character literal - needless to say that is not going to work as expected. A string is a string even if there is only one character in it. As advised - use either the [] operator or better off for reading - QString::at() const which is guaranteed to create no extra copy. You don't use it on the QFile, nor on the QTextStream, but on the QString that is returned from the read() method of the text stream targeted at the file.

Once you have the text in memory, you can either use the regular QString methods like indexOf() to search for the index of a contained character.

0
votes

in want to find the "$" sign so i will realize that I've reached the number.

It sounds to me that you're searching for the '$' symbol because you're more interested in the dollar value that follows it. In this case, I suggest reading the files line by line and running them through a QRegExp to extract any values you're looking for.

QRegExp dollarFind("\\$(\\d+)");
while(!myfile_stream.atEnd()){
    QString line = myfile_stream.readLine();
    if (dollarFind.exactMatch(line)){
        QStringList dollars = dollarFind.capturedTexts();
        qDebug() << "Dollar values found: " << dollars.join(", ");
    }
}