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

QVector

Scheduled Pinned Locked Moved General and Desktop
31 Posts 9 Posters 11.2k 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.
  • M Offline
    M Offline
    mlong
    wrote on last edited by
    #15

    You may want to open your file before trying to read from it.

    Software Engineer
    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pbhuter
      wrote on last edited by
      #16

      Well, obviously - why didn't I see that. However, infile.open() doesn't work. I apparently need some arguments. I looked in the online Qt documentation for QFile, and I don't see anything about "open".

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mlong
        wrote on last edited by
        #17

        QFile inherits "open":http://doc.qt.nokia.com/4.7-snapshot/qiodevice.html#open from QIODevice. You want
        @
        infile.open(QIODevice::ReadOnly);
        @

        Software Engineer
        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pbhuter
          wrote on last edited by
          #18

          I did

          @infile.open(QIODevice::ReadOnly)@

          But I'm still having the same issue.

          1 Reply Last reply
          0
          • Z Offline
            Z Offline
            ZapB
            wrote on last edited by
            #19

            Please post a small compilable example that reproduces your problem. Then we can try it out ourselves and will likely be able to help you much easier.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pbhuter
              wrote on last edited by
              #20

              @
              QFile infile(_filename);

              infile.open(QIODevice::ReadOnly);

              QTextStream its(&infile;);

              // parse header
              bool dataRow = false;

              QString line;

              while (!dataRow && !its.atEnd())
              {
              line = its.readLine();

              if (line.contains(QRegExp("\\d+\\.?\\d*"))) // check for data row
              {
                dataRow = true;
              }
              

              } //should have first row of data

              _stages = line.toInt();@

              I get into the while loop and it seems to think its is empty, so really I'm thinking it's still an issue with QTextStream. Let me know if you need anything else.

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                ZapB
                wrote on last edited by
                #21

                And what is in your data file please?

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  pbhuter
                  wrote on last edited by
                  #22

                  test.dat contains:

                  2
                  100
                  50
                  50
                  2.5
                  5
                  60
                  10
                  75
                  45
                  30
                  1.6
                  2
                  45
                  25
                  25
                  15

                  So _stages should pick up the 2 at the top of the file. I then have:

                  @while (!its.atEnd())
                  {
                  int i = 0;
                  while(i < _stages)
                  {
                  line = its.readLine();
                  _wMasses.push_back(line.toDouble());

                  line = its.readLine();
                  _fMasses.push_back(line.toDouble());

                  line = its.readLine();
                  _dMasses.push_back(line.toDouble());

                  line = its.readLine();
                  _flowRates.push_back(line.toDouble());

                  line = its.readLine();
                  _burnTimes.push_back(line.toDouble());

                  line = its.readLine();
                  _thrusts.push_back(line.toDouble());

                  line = its.readLine();
                  _alphas.push_back(line.toDouble());

                  i++;
                  }

                  line = its.readLine();
                  _Mass = line.toDouble();

                  line = its.readLine();
                  _Alpha = line.toDouble();
                  }@

                  Which reads the rest of the file.

                  1 Reply Last reply
                  0
                  • Z Offline
                    Z Offline
                    ZapB
                    wrote on last edited by
                    #23

                    Does it work if you change this line:

                    @
                    infile.open(QIODevice::ReadOnly);
                    @

                    to this:

                    @
                    infile.open(QIODevice::ReadOnly | QIODevice::Text);
                    @

                    Nokia Certified Qt Specialist
                    Interested in hearing about Qt related work

                    1 Reply Last reply
                    0
                    • P Offline
                      P Offline
                      pbhuter
                      wrote on last edited by
                      #24

                      No, still doesn't work...

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        pbhuter
                        wrote on last edited by
                        #25

                        I had:

                        @if (!(infile.open(QIODevice::ReadOnly)))
                        {
                        return 1;
                        }@

                        And it goes in and returns 1. Not sure what's going on.

                        1 Reply Last reply
                        0
                        • Z Offline
                          Z Offline
                          ZapB
                          wrote on last edited by
                          #26

                          Does your process have permissions to read the file? Is some other process locking the file? Is the path correct?

                          Nokia Certified Qt Specialist
                          Interested in hearing about Qt related work

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            pbhuter
                            wrote on last edited by
                            #27

                            My file was either in the wrong location, or I was trying to run my program from the wrong directory. Thank you so much.

                            1 Reply Last reply
                            0
                            • Z Offline
                              Z Offline
                              ZapB
                              wrote on last edited by
                              #28

                              Meh, we've all been there, done that at some point. :-) Another trick you can use is to use something like this:

                              @
                              QFileInfo fi( fileName );
                              if ( !fi.isReadable() )
                              do something
                              @

                              Glad you got it working in the end.

                              Nokia Certified Qt Specialist
                              Interested in hearing about Qt related work

                              1 Reply Last reply
                              0
                              • P Offline
                                P Offline
                                pbhuter
                                wrote on last edited by
                                #29

                                Reading input files is always something I struggle with. Now I can get down to the business of writing the code to actually do stuff. Thanks again for everyone's help!

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

                                  You might want to add some error checking in your parsing code... It is not very save to just assume that your reading and converting worked properly, if you have no control over the input file...

                                  1 Reply Last reply
                                  0
                                  • K Offline
                                    K Offline
                                    KA51O
                                    wrote on last edited by
                                    #31

                                    another option would be to check if the file you want to open exists before trying to open it.
                                    Just use something like
                                    @if(QFile::exists(filename))
                                    {
                                    QFile file (filename);
                                    if(file.open(QIODevice::ReadOnly)
                                    {
                                    QByteArray strFileContent = file.readAll();
                                    }
                                    }@

                                    This way its easier to find the error when debugging.

                                    PS: Any possible errors are mine and are not to be used by anyone else.

                                    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