Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ifstream and QString

ifstream and QString

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 946 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndrzejB
    wrote on last edited by
    #1

    I read all lines to vector using ifstream and string:

    vector<string> readLines(string filename)
    {
        ifstream infile(filename);
        vector<string> v;
        string line;
        bool readedNewline;
        if (infile.peek() != EOF)
        while(true) {
            readedNewline = getline(infile, line).good();
            v.push_back(line);
            if (!readedNewline) break;
        }
        return v;
    }
    

    Now I want replace string by QString; I must change ifstream and getline?

    JonBJ 1 Reply Last reply
    0
    • A AndrzejB

      but if it's just your own stuff I would use Qt calls instead for everything.

      Which Qt class I can use instead of std:ifstream?

      And why do you want to store an extra blank item at the end of the vector

      This is not required for most cases but sometimes, I want imitate in QString vector text file structure accurate if is possible. I want distinguish between last line without and with \n. If I save this vector to second file , this two files should be identical.

      M Offline
      M Offline
      mpergand
      wrote on last edited by
      #4

      @AndrzejB said in ifstream and QString:

      Which Qt class I can use instead of std:ifstream?

          QFile file(filename);
          QVector<QString> vec;
      
          if(file.open(QFile::ReadOnly | QFile::Text))
              {
              while(!file.atEnd())
                  {
                  vec<<file.readLine();
                  }
              }
          else
              {
              qDebug()<<file.errorString()<<file.error();
              }
      
      1 Reply Last reply
      2
      • A AndrzejB

        I read all lines to vector using ifstream and string:

        vector<string> readLines(string filename)
        {
            ifstream infile(filename);
            vector<string> v;
            string line;
            bool readedNewline;
            if (infile.peek() != EOF)
            while(true) {
                readedNewline = getline(infile, line).good();
                v.push_back(line);
                if (!readedNewline) break;
            }
            return v;
        }
        

        Now I want replace string by QString; I must change ifstream and getline?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @AndrzejB
        It's up to you, but why are using std:: for everything (I/O, vectors, strings) if you want to interoperate with Qt? If you have a large body of outside code using that I can understand, but if it's just your own stuff I would use Qt calls instead for everything. Else you are wasting time converting between std & Qt classes.

        On a separate point, do you not think your code for reading is a bit bloated? And why do you want to store an extra blank item at the end of the vector when you reach end of file? Even if you stick with std instead of Qt for file handling, what was wrong with:

        while (getline(infile, line))
            v.push_back(line);
        

        instead of all 7 lines of yours?

        1 Reply Last reply
        2
        • A Offline
          A Offline
          AndrzejB
          wrote on last edited by
          #3

          but if it's just your own stuff I would use Qt calls instead for everything.

          Which Qt class I can use instead of std:ifstream?

          And why do you want to store an extra blank item at the end of the vector

          This is not required for most cases but sometimes, I want imitate in QString vector text file structure accurate if is possible. I want distinguish between last line without and with \n. If I save this vector to second file , this two files should be identical.

          M 1 Reply Last reply
          0
          • A AndrzejB

            but if it's just your own stuff I would use Qt calls instead for everything.

            Which Qt class I can use instead of std:ifstream?

            And why do you want to store an extra blank item at the end of the vector

            This is not required for most cases but sometimes, I want imitate in QString vector text file structure accurate if is possible. I want distinguish between last line without and with \n. If I save this vector to second file , this two files should be identical.

            M Offline
            M Offline
            mpergand
            wrote on last edited by
            #4

            @AndrzejB said in ifstream and QString:

            Which Qt class I can use instead of std:ifstream?

                QFile file(filename);
                QVector<QString> vec;
            
                if(file.open(QFile::ReadOnly | QFile::Text))
                    {
                    while(!file.atEnd())
                        {
                        vec<<file.readLine();
                        }
                    }
                else
                    {
                    qDebug()<<file.errorString()<<file.error();
                    }
            
            1 Reply Last reply
            2

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved