Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7653557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:03:42+00:00 2026-05-31T12:03:42+00:00

I read two QStrings (ValueOne,ValueTwo) out of a file with (just a basic example)

  • 0

I read two QStrings (ValueOne,ValueTwo) out of a file with (just a basic example)

int main(int argc, char *argv[]) {
    QString x = ("yes " 
                 "start ValueOne end" 
                 "no" 
                 "start ValueTwo end");

   //try to read ValueOne
    QString s = "start";
    QString e = "end";
    int start = x.indexOf(s, 0, Qt::CaseInsensitive); 
    int end = x.indexOf(e, Qt::CaseInsensitive); 

    if(start != -1){ 

        QString y = x.mid(start + s.length(), (end - (start + s.length()))); 

        qDebug() << y << (start + s.length()) << (end - (start + s.length()));

    //try to read ValueTwo
    QString s2 = "start";
    QString e2 = "end";
    int start2 = x.indexOf(s2, 0, Qt::CaseInsensitive); 
    int end2 = x.indexOf(e2, Qt::CaseInsensitive); 

    if(start2 != -1){ 

        QString y2 = x.mid(start2 + s.length2(), (end2 - (start2 + s.length2()))); 

        qDebug() << y2 << (start2 + s.length2()) << (end2 - (start2 + s.length2()));
    }
}

As you see the source code can’t difference between ValueOne and ValueTwo just by “start” and “end”, because both QString::mid() methods (which proceed line by line as far as I know) have the same start position and the same length (see http://qt-project.org/doc/qt-4.8/qstring.html#mid ). Therefore I thought if the whole string was one line like

QString x = "yes start ValueOne end no start ValueTwo end ";

I could difference between the two values with QString s = “yes start” and QString s2 = “no start”. So would convert the multiline String into a one line String be a solution and how can I do this? or Is there another maybe better solution?
greetings

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:03:43+00:00Added an answer on May 31, 2026 at 12:03 pm

    As I already mention in your other question I would prefer QRegExp. It seems to be more readable.

    If your first string is the 2n value always and your second string is 2n+1 you could use the modulo operator:

    #include <QDebug>
    #include <QString>
    #include <QStringList>
    #include <QRegExp>
    
    int main()
    {
        QString x = ("yes \nstart ValueOne end \nno \nstart ValueTwo end\n"
                    "yes \nstart ValueThree end \nno \nstart ValueFour end ");
    
        QStringList y1;
        QStringList y2;
    
        // create regular expression
        QRegExp rx("start\\s+(.+)\\s+end\\s+", Qt::CaseInsensitive);
    
        // don't try to get the largest match (start ValueOne ... ValueFour end)
        // minimal match should be (start ValueOne end)
        rx.setMinimal(true);
    
        int pos=0;
        int i=0; // counter
    
        // look for possible matches
        QString match;
        while ((pos=rx.indexIn(x, pos)) != -1) {
            i+=1; // increase counter for every match
            match=rx.cap(1); // get first match in (.+)
    
            // use modulo to distinguish between y1/y2    
            if (i % 2) {
                y1 << match;
            } else {
                y2 << match;
            }
    
            pos+=rx.matchedLength();
        }
    
        qDebug() << "y1:" << y1;
        qDebug() << "y2:" << y2;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a source code like: int main(int argc, char *argv[]) { QFile
which one of the two is more spread? I want to read out the
Is there a way to read two lines of a csv file at a
I have to read a binary file. I have to read two type of
How I can read two values, one line in the txt file? value1 ,
I just read two articles over this topic which provide infomration inconsistent, so I
I have a bash script as below, and I want it to read two
I've read through the two other threads that extract the dll from the application
I have two grails servers: Server - has read/write access to the database Web
I have two (UNIX) programs A and B that read and write from stdin/stdout.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.