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?
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
17 Posts 4 Posters 10.6k 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.
  • P Offline
    P Offline
    pratik041
    wrote on last edited by
    #1

    While i am reading a line from a file with text containing \n Character and storing it in a string of type Qstring. If i am using that Qstring object to set tooltip why i am not getting the tooltip with newline character?

    Pratik Agrawal

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

      I suspect you are using a readline approach, so you are getting the content of the line, not the formatted text of the line (i.e., with the \n). Otherwise please show your code.

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

        [quote author="fluca1978" date="1323942903"]I suspect you are using a readline approach, so you are getting the content of the line, not the formatted text of the line (i.e., with the \n). Otherwise please show your code.[/quote]

        ya i am using readline approach. So what should i use instead?

        Pratik Agrawal

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

          Place back a newline into the final qstring each time you end a readline.

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

            [quote author="fluca1978" date="1323944412"]Place back a newline into the final qstring each time you end a readline.[/quote]

            i didn't get it. Can you please specify in more detail?

            Pratik Agrawal

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              When using readline to read text from a file, the actual end-of-line marker is stripped from the result you get. Either put it back yourself by simply appending a new end-of-line marker to the results of each of your readline calls, or read the file in a different way.

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

                [quote author="Andre" date="1323945302"]When using readline to read text from a file, the actual end-of-line marker is stripped from the result you get. Either put it back yourself by simply appending a new end-of-line marker to the results of each of your readline calls, or read the file in a different way. [/quote]

                "Read file in different way" means which other function i can use in place of that?

                Pratik Agrawal

                1 Reply Last reply
                0
                • 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