Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, May 2007
how to read lines of file into QString lines[]


Message 1 in thread

hi,all
i use this to read lines :
        QFile file("tmp.txt");
        QPtrList<QString> lines;
        if ( file.open( IO_ReadOnly ) ) {
                QTextStream stream( &file );
                QString line;
                while ( !stream.atEnd() ) {
                        line = stream.readLine(); // line of text excluding '\n'
                        lines.append(&line);
                }
                file.close();
        }
and i want to manipulate each line in lines:
for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
{
          if ( *it.left(1)="#")
     //do something
but gcc said
error: 'class QValueListIterator<QString>' has no member named 'left'
so *it is not QString type?
how can i get each line in lines with a type QString?

regards

--
 [ signature omitted ] 

Message 2 in thread

i think there are some problems in u code:
first no QPtrList in Qt4, and in Qt3 it called Q3PtrList. I also can't
find the key word IO_ReadOnly ,u can use QIODevice::WriteOnly instead
it .
second : use QList<QString *> instead of QPtrList<QString> and
QList<QString*>::Iterator it instead of QStringList::Iterator 
thrid : call (*it)->left(1) == "#" not *it.left(1) = "#"

å 2007-05-06æç 23:03 +0800ïjiang jefixåéï
> hi,all
> i use this to read lines :
>         QFile file("tmp.txt");
>         QPtrList<QString> lines;
>         if ( file.open( IO_ReadOnly ) ) {
>                 QTextStream stream( &file );
>                 QString line;
>                 while ( !stream.atEnd() ) {
>                         line = stream.readLine(); // line of text excluding '\n'
>                         lines.append(&line);
>                 }
>                 file.close();
>         }
> and i want to manipulate each line in lines:
> for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
> {
>           if ( *it.left(1)="#")
>      //do something
> but gcc said
> error: 'class QValueListIterator<QString>' has no member named 'left'
> so *it is not QString type?
> how can i get each line in lines with a type QString?
> 
> regards
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
> 

--
 [ signature omitted ] 

Message 3 in thread

On 06.05.07 23:25:29, sjtu wrote:
> i think there are some problems in u code:
> first no QPtrList in Qt4, and in Qt3 it called Q3PtrList. I also can't

No, Its QPtrList in Qt3 and Q3PtrList in Qt4.

> find the key word IO_ReadOnly ,u can use QIODevice::WriteOnly instead
> it .

Thats wrong, in Qt4 its QIODevice::ReadOnly, IO_ReadOnly comes from Qt3.

> second : use QList<QString *> instead of QPtrList<QString> and
> QList<QString*>::Iterator it instead of QStringList::Iterator 
> thrid : call (*it)->left(1) == "#" not *it.left(1) = "#"

No, use QStringList instead of a list of pointers to QString. There's
nearly never a reason to have pointers to QStrings.

Andreas

-- 
 [ signature omitted ] 

Message 4 in thread

On 06.05.07 23:03:49, jiang jefix wrote:
> hi,all
> i use this to read lines :
>        QFile file("tmp.txt");
>        QPtrList<QString> lines;
>        if ( file.open( IO_ReadOnly ) ) {
>                QTextStream stream( &file );
>                QString line;
>                while ( !stream.atEnd() ) {
>                        line = stream.readLine(); // line of text excluding '\n'
>                        lines.append(&line);
>                }
>                file.close();
>        }
> and i want to manipulate each line in lines:
> for ( QStringList::Iterator it = lines.begin(); it != lines.end(); ++it )
> {
>          if ( *it.left(1)="#")
>     //do something
> but gcc said
> error: 'class QValueListIterator<QString>' has no member named 'left'
> so *it is not QString type?

This has nothing to do with Qt, its a plain C++ error. The .left() is
evaluated before the * in front of it, thats why what happens is that
the code tries to call QValueListIterator<QString>::left(). Just add
some parenthesis around the *it and you're done.

Also I don't think you want a QPtrList<QString>, a QStringList should be
fine.

Andreas

-- 
 [ signature omitted ] 

Message 5 in thread

hey,it is!
thanks a lot

--
 [ signature omitted ] 

Message 6 in thread

> -----Original Message-----
> From: Andreas Pakulat [mailto:apaku@xxxxxx]
> Sent: Sunday, May 06, 2007 8:28 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: how to read lines of file into QString lines[]
> 
> On 06.05.07 23:03:49, jiang jefix wrote:
> > hi,all
> > i use this to read lines :
> >        QFile file("tmp.txt");
> >        QPtrList<QString> lines;
> >        if ( file.open( IO_ReadOnly ) ) {
> >                QTextStream stream( &file );
> >                QString line;
> >                while ( !stream.atEnd() ) {
> >                        line = stream.readLine(); // line of text
> excluding '\n'
> >                        lines.append(&line);
> >                }
> >                file.close();
> >        }
> > and i want to manipulate each line in lines:
> > for ( QStringList::Iterator it = lines.begin(); it != lines.end();
++it
> )
> > {
> >          if ( *it.left(1)="#")
> >     //do something
> > but gcc said
> > error: 'class QValueListIterator<QString>' has no member named
'left'
> > so *it is not QString type?
> 
> This has nothing to do with Qt, its a plain C++ error. The .left() is
> evaluated before the * in front of it, thats why what happens is that
> the code tries to call QValueListIterator<QString>::left(). Just add
> some parenthesis around the *it and you're done.
> 
> Also I don't think you want a QPtrList<QString>, a QStringList should
be
> fine.
> 
Also... Your code wont work (Ops code... not Andreas's), since your
storing a pointer to a local variable (line in this case) each time the
while loop executes, it will overwrite the original value....

Another C++ error...



--
 [ signature omitted ] 

Message 7 in thread

Please don't email me directly....

It should work just fine.

Scott

> -----Original Message-----
> From: jiang jefix [mailto:jefix214@xxxxxxxxx]
> Sent: Sunday, May 06, 2007 9:02 AM
> To: Scott Aron Bloom
> Subject: Re: how to read lines of file into QString lines[]
> 
> oh yes,thanks
> will this ok?
> 
>         QFile file("tmp.txt");
>         QStringList lines;
>         if ( file.open( IO_ReadOnly ) ) {
>                 QTextStream stream( &file );
>                 QString line;
>                 while ( !stream.atEnd() ) {
>                         line = stream.readLine();
>                         lines += line;
>                 }
> 
> 
> 2007/5/6, Scott Aron Bloom <scott@xxxxxxxxxxxx>:
> > > -----Original Message-----
> > > From: Andreas Pakulat [mailto:apaku@xxxxxx]
> > > Sent: Sunday, May 06, 2007 8:28 AM
> > > To: qt-interest@xxxxxxxxxxxxx
> > > Subject: Re: how to read lines of file into QString lines[]
> > >
> > > On 06.05.07 23:03:49, jiang jefix wrote:
> > > > hi,all
> > > > i use this to read lines :
> > > >        QFile file("tmp.txt");
> > > >        QPtrList<QString> lines;
> > > >        if ( file.open( IO_ReadOnly ) ) {
> > > >                QTextStream stream( &file );
> > > >                QString line;
> > > >                while ( !stream.atEnd() ) {
> > > >                        line = stream.readLine(); // line of text
> > > excluding '\n'
> > > >                        lines.append(&line);
> > > >                }
> > > >                file.close();
> > > >        }
> > > > and i want to manipulate each line in lines:
> > > > for ( QStringList::Iterator it = lines.begin(); it !=
lines.end();
> > ++it
> > > )
> > > > {
> > > >          if ( *it.left(1)="#")
> > > >     //do something
> > > > but gcc said
> > > > error: 'class QValueListIterator<QString>' has no member named
> > 'left'
> > > > so *it is not QString type?
> > >
> > > This has nothing to do with Qt, its a plain C++ error. The .left()
is
> > > evaluated before the * in front of it, thats why what happens is
that
> > > the code tries to call QValueListIterator<QString>::left(). Just
add
> > > some parenthesis around the *it and you're done.
> > >
> > > Also I don't think you want a QPtrList<QString>, a QStringList
should
> > be
> > > fine.
> > >
> > Also... Your code wont work (Ops code... not Andreas's), since your
> > storing a pointer to a local variable (line in this case) each time
the
> > while loop executes, it will overwrite the original value....
> >
> > Another C++ error...
> >
> >
> >
> > --
> > To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx
with
> "unsubscribe" in the subject or the body.
> > List archive and information:
http://lists.trolltech.com/qt-interest/
> >
> >

--
 [ signature omitted ]