input in QT4

Hi,

I am new to QT4 and I am having a hard time reading an input file line by line. I know how to do this in C++ but I can’t figure it out in QT4. My code is included below. Any help would be greatly appreciated.

#include <QDate>
#include <QFile>
#include <QTextStream>
#include <string>
#include <qstd.h>
#include <stdlib.h>

using namespace qstd;
QTextStream cout(stdout);

int main(int argc, const char *ARGV]) {

        if(argc == 1) {
            QFile file("birthdays.dat");
            QTextStream in(&file);
            while(!in.atEnd()) {
                    QString line = in.readLine();
                    cout << line << endl;
            }

        return 0;
    }

On Thu, 20 Jan 2011 01:36:02 +0000, vthokie11 wrote:

> I know how to do this in C++ but I can’t figure it out in QT4.

You might include some debug code in your code to identify where the
failure is. That might put you on a path to understanding why your code
isn’t working right.

I spent a fair amount of time working as a lab assistant back when I was
in college, and that was one fundamental error most students starting out
with coding (in any language) ran into - they assumed the end result of
their assignment was the only code they could add in. Many benefited
from understanding that adding runtime debug code can help isolate issues
like the one you describe.

You haven’t said that you’re a student, but as I don’t know QT4
programming, it doesn’t really matter - I’d give that advice whether you
were a student working on an assignment or not.

That said, if you are a student working on an assignment, remember that
while we’re here to help, we’re not here to do your homework for you. :slight_smile:

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

Hi,

I am a student but this is not an assignment. I am not a strong programmer when it comes to software applications (I much prefer hardware) so I am trying to get a head start on my Software Engineering course that I am currently enrolled in. Also, I had debug code included but felt that it was not necessary to include it when submitting the code to this forum, I found that the code never enters the while loop. That being said I am not asking anyone to do my homework for me.

Thanks

vthokie11 wrote:

>
> Hi,
>
> I am a student but this is not an assignment. I am not a strong
> programmer when it comes to software applications (I much prefer
> hardware) so I am trying to get a head start on my Software Engineering
> course that I am currently enrolled in. Also, I had debug code included
> but felt that it was not necessary to include it when submitting the
> code to this forum, I found that the code never enters the while loop.
> That being said I am not asking anyone to do my homework for me.
>
> Thanks
>
>
You never open the file for reading in your code.
You construct a file object QFile file(“birthdays.dat”) but do not open it
with file.open
Look at the QFile online help for details.


openSUSE 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.5 | GeForce
9600 GT | 4GB Ram
openSUSE 11.3 64 bit | Intel Core2 Duo T9300@2.50GHz | KDE 4.5 | Quadro FX
3600M | 4GB Ram

I’ll try this out when I get the opportunity…I was under the impression that creating the file object also opened the file as well. I’ll let you know if this solves my problem. Thanks for your help.

Problem Solved! It was an issue with the file not being opened…thanks so much for your help! I have included my code in case someone else runs into this issue!

#include <QDate>
#include <QFile>
#include <QTextStream>
#include <string>
#include <qstd.h>
#include <stdlib.h>

using namespace qstd;
QTextStream cout(stdout);

int main(int argc, const char *ARGV]) {

        if(argc == 1) {
            QFile file("birthdays.dat");
            if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
                return 1;
            }
            QTextStream in(&file);
            while(!in.atEnd()) {
                    QString line = in.readLine();
                    cout << line << endl;
            }
        }

        return 0;
    }
   

vthokie11 wrote:

>
> Problem Solved! It was an issue with the file not being opened…thanks
> so much for your help! I have included my code in case someone else runs
> into this issue!

Glad to see you solved it. Just an additional hint, always close a file when
you are finished with it to release the resources with file.close().
Not sure if you need it in this case but at least it is good style and
avoids problems if you have a larger program.


openSUSE 11.3 64 bit | Intel Core2 Quad Q8300@2.50GHz | KDE 4.5 | GeForce
9600 GT | 4GB Ram
openSUSE 11.3 64 bit | Intel Core2 Duo T9300@2.50GHz | KDE 4.5 | Quadro FX
3600M | 4GB Ram

On Thu, 20 Jan 2011 15:06:01 +0000, vthokie11 wrote:

> I am a student but this is not an assignment. I am not a strong
> programmer when it comes to software applications (I much prefer
> hardware) so I am trying to get a head start on my Software Engineering
> course that I am currently enrolled in. Also, I had debug code included
> but felt that it was not necessary to include it when submitting the
> code to this forum, I found that the code never enters the while loop.
> That being said I am not asking anyone to do my homework for me.

No problem. :slight_smile:

Are you using the book in the Bruce Perens series that includes
information on coding for QT4 (I forget the name, but I believe it is a
book used for one of the classes in the software engineering curriculum
where you are at school)?

One thing that’s fairly common is for C++ functions (and programming in
QT4 is still C++, just C++ with a set of libraries for QT4) to return a
value that explains what’s happening when something goes wrong.

In your case, you’re exiting the loop prematurely, which means the while
condition is being met before it does the first thing.

I would look at what the atEnd() function returns if it’s executed before
reading anything in the file. We can suppose that the expression “!
in.atEnd()” is returning the value that is terminating the loop, and
because of the way you’ve structure things, it’s not getting to the code
inside the loop.

So there’s two potential issues here to investigate:

  1. That the condition of ending the while loop is returning a FALSE
    value when it’s not expected to.

  2. That there’s a structural/logic problem in how you’ve structured the
    code.

From a standpoint of debugging, it would be a good exercise to rewrite
the code so that you are capturing the value evaluated by “!in.atEnd()”
so you can display that value. That will probably help you structure
things so the contents of the loop execute at least once.

Does that help?

Jim

Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C