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. Problem in reading a '\n' character from a file?
Qt 6.11 is out! See what's new in the release blog

Problem in reading a '\n' character from a file?

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 10.7k Views 1 Watching
  • 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
    andre
    wrote on last edited by
    #8

    Yeah, for instance, read the whole file in one go (if it is not too big).

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pratik041
      wrote on last edited by
      #9

      [quote author="Andre" date="1323946497"]Yeah, for instance, read the whole file in one go (if it is not too big). [/quote]

      sorry, I can't do that because it is a big file. Is their any other way?

      Pratik Agrawal

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #10

        You should have something like:

        @
        QTextStream stream( &myFile );
        while( ! stream.atEnd() )
        line = line + stream.readLine() + "\n";
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Seba84
          wrote on last edited by
          #11

          Hi Fluca,
          Your code should look something like this:

          @QString str;
          QFile file("file.txt");
          if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
          QTextStream in(&file);
          do {
          // Puts text into your string and adds the newline character
          str += in.readLine() + "\n";
          } while(!in.atEnd());
          }@

          Regards,
          Sebastian

          1 Reply Last reply
          0
          • P Offline
            P Offline
            pratik041
            wrote on last edited by
            #12

            @
            QTextStream stream( &myFile );
            while( ! stream.atEnd() )
            line = line + stream.readLine() + "\n";
            @

            this code will handle newline character but if in my file i have also line like the following
            @Conference scheduled on %s at %s with %s is cancelled@
            This line is using formatted string. So, i want something which will read data as string. So that after
            reading the data from file i can still do any string operation on it.

            Pratik Agrawal

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Seba84
              wrote on last edited by
              #13

              Do you know that QString has an .arg() function for doing operations?
              Check out the manual.

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fluca1978
                wrote on last edited by
                #14

                [quote author="Seba84" date="1323946699"]Hi Fluca,
                Your code should look something like this:

                @QString str;
                QFile file("file.txt");
                if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                QTextStream in(&file);
                do {
                // Puts text into your string and adds the newline character
                str += in.readLine() + "\n";
                } while(!in.atEnd());
                }@

                Regards,
                Sebastian[/quote]

                My code was a quick on-forum example to specify that at each iteration the "\n" can be added.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fluca1978
                  wrote on last edited by
                  #15

                  [quote author="pratik041" date="1323948661"]

                  this code will handle newline character but if in my file i have also line like the following
                  @Conference scheduled on %s at %s with %s is cancelled@
                  This line is using formatted string. So, i want something which will read data as string. So that after
                  reading the data from file i can still do any string operation on it. [/quote]

                  It is not clear to me what you are trying to achieve. If you want to read the string as is (i.e., with %) then it is fine. If you need to read the line with interpolated values then you have to set values in a different way, not reading the line by file.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    Seba84
                    wrote on last edited by
                    #16

                    Sorry Fluca, I mistaken the name, I was talking to practik041. The post I did was practically at the same time as yours, I saw yours after posting it.

                    Practik,
                    You do not want to understand. Can you check the manual?
                    You can handle %s with the QString::arg() function, definition:

                    @QString QString::arg(const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) @

                    Here it is an example from the QT Help:
                    @QString i; // current file's number
                    QString total; // number of files to process
                    QString fileName; // current file's name

                    QString status = QString("Processing file %1 of %2: %3").arg(i).arg(total).arg(fileName);@

                    See the manual for more info.

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pratik041
                      wrote on last edited by
                      #17

                      [quote author="Seba84" date="1323954519"]Sorry Fluca, I mistaken the name, I was talking to practik041. The post I did was practically at the same time as yours, I saw yours after posting it.

                      Practik,
                      You do not want to understand. Can you check the manual?
                      You can handle %s with the QString::arg() function, definition:

                      @QString QString::arg(const QString & a, int fieldWidth = 0, const QChar & fillChar = QLatin1Char( ' ' ) ) @

                      Here it is an example from the QT Help:
                      @QString i; // current file's number
                      QString total; // number of files to process
                      QString fileName; // current file's name

                      QString status = QString("Processing file %1 of %2: %3").arg(i).arg(total).arg(fileName);@

                      See the manual for more info.[/quote]
                      I have seen the manual. It is clear to me.

                      Pratik Agrawal

                      1 Reply Last reply
                      0

                      • Login

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