Results 1 to 19 of 19

Thread: xstrm.read();

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default xstrm.read();

    Hi, Do anyone know why this instruction gets an runtime error and a console error like as
    QFile::getch: Read operation not permitted ? Thanks
    Qt Code:
    1. QString plainText = xstrm.read();
    To copy to clipboard, switch view to plain text mode 

    I need to do this below but I've got some problems....
    Qt Code:
    1. QString plainText = xstrm.readline();
    2. //cout << "plainText " << plainText << endl;
    3. QDomText t1 = doc.createTextNode(plainText);
    4. tag1.appendChild(t1);
    To copy to clipboard, switch view to plain text mode 
    If I use .readline() seem works but the text inside TAG (in the file) appear null (string null). Why? However I need to read entire stream and not a line...
    Last edited by mickey; 2nd July 2006 at 18:59.
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    What is that "xstrm" variable?

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: xstrm.read();

    Qt Code:
    1. QFile f(file);
    2. f.open(IO_WriteOnly);
    3. QTextStream xstrm(&f);
    To copy to clipboard, switch view to plain text mode 
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    Quote Originally Posted by mickey
    QFile::getch: Read operation not permitted ?
    ...
    f.open(IO_WriteOnly);
    Do you see where the problem is?

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: xstrm.read();

    yes it was the error; but if I use .read() my app doesn't respond....
    I coded this also:
    Qt Code:
    1. QFile f(file);
    2. f.open(IO_ReadOnly | IO_WriteOnly);
    3. QTextStream xstrm(&f);
    4. Class.Write(QTextStream(&f));
    5. QString plainText = xstrm.readLine(); // or .read()
    6. QDomText t1 = doc.createTextNode(plainText);
    7. //QDomText t1 = doc.createTextNode("hello"); //with this ok
    8. tag1.appendChild(t1);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Class::Write(QTextStream &xstrm) {
    2. xstrm << "hello\n";
    3. }
    To copy to clipboard, switch view to plain text mode 
    after this in my file I found:
    Qt Code:
    1. hello
    2. a line of ascii code
    To copy to clipboard, switch view to plain text mode 
    why? "hello" is right...with "QDomText t1 = doc.createTextNode(plainText)" I'm trying to insert again "hello" in the file.....thanks
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    I guess that QTextStream just reads the file from the current position, try adding "f.reset()" before xstrm.readLine().

  7. #7
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: xstrm.read();

    ok now it seems work with read() properly. but a strange thing happen in the file after write; the second part of file appear in a wrong way..
    Qt Code:
    1. &lt;Light0 pos=&quot;4 1 4 1&quot; //wrong
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. <Light0 pos="4 1 4 1" //right
    To copy to clipboard, switch view to plain text mode 
    Why this? thanks
    Regards

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    Quote Originally Posted by mickey
    Why this? thanks
    Maybe because you create a text node (i.e. text that is between tags)?
    Could you describe what are you trying to achieve?

  9. #9
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: xstrm.read();

    Quote Originally Posted by mickey
    yes it was the error; but if I use .read() my app doesn't respond....
    I coded this also:
    Qt Code:
    1. QFile f(file);
    2. f.open(IO_ReadOnly | IO_WriteOnly);
    3. QTextStream xstrm(&f);
    4. QDomElement tag1 = doc.createElement ("PLAIN"); // SEE THIS!!!!
    5. Class.Write(QTextStream(&f));
    6. QString plainText = xstrm.readLine(); // or .read()
    7. QDomText t1 = doc.createTextNode(plainText);
    8. //QDomText t1 = doc.createTextNode("hello"); //with this ok
    9. tag1.appendChild(t1);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Class::Write(QTextStream &xstrm) {
    2. xstrm << "hello\n";
    3. }
    To copy to clipboard, switch view to plain text mode 
    after this in my file I found:
    Qt Code:
    1. hello
    2. a line of ascii code
    To copy to clipboard, switch view to plain text mode 
    like above I'm trying to create a file; in that I like put "hello" with class.XML (this work); then I like put the contentent of file (in this case "hello") inside two tag <Plain> </plain>;
    that doesn't work (but it works using
    Qt Code:
    1. QDomText t1 = doc.createTextNode("hello");
    To copy to clipboard, switch view to plain text mode 

    other thing: using f.open(IO_ReadOnly | IO_WriteOnly); when I save file the text is append at the end...why? with only IO_WriteOnly the file is destroyed everytime (I want this)...thanks

    then I need put the
    Qt Code:
    1. QDomElement tag1 = doc.createElement ("PLAIN");
    2. Class.Write(QTextStream(&f));
    3. QDomText t1 = doc.createTextNode(..............);
    To copy to clipboard, switch view to plain text mode 
    content of strm between tag PLAIN. How this?
    Last edited by mickey; 3rd July 2006 at 18:36.
    Regards

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    Quote Originally Posted by mickey
    then I need put the content of strm between tag PLAIN. How this?
    If xstrm contains text everything is OK --- < & > were just converted to entities, but if xstrm contains text and XML tags, then you can't use a text node to add it to the document.

    Consider this fragment of XML document:
    ...
    xxx
    <tag>yyy</tag>
    zzz
    ...
    Here you have 4 nodes. Three text nodes ("xxx", "yyy" and "zzz") and element node "tag". You can't use a single node instead of 4 nodes.

    Why do you need to read data from that file? Maybe you could create a DOM document and then just write it to a file?

  11. #11
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: xstrm.read();

    ok I'll reformulate my problem:

    Qt Code:
    1. QFile f(file);
    2. f.open(IO_WriteOnly);
    3. QTextStream xstrm(&f);
    4. QDomDocument doc(file);
    5. QDomElement root = doc.createElement (base_file);
    6. doc.appendChild (root);
    7. QDomElement tag1 = doc.createElement ("PLAIN Property");
    8. root.appendChild(tag1);
    9. scene.p.XMLWrite(QTextStream(&f));
    10. Class.Write(QTextStream(&f));
    11. QDomText t1 = doc.createTextNode("text");
    12. QString xml = doc.toString();
    13. xstrm << xml;
    To copy to clipboard, switch view to plain text mode 
    this produce a file:
    Qt Code:
    1. hello
    2. <!DOCTYPE level.xml>
    3. <le>
    4. <PLAIN Property> HI </PLAIN Property>
    5. </le>
    To copy to clipboard, switch view to plain text mode 
    hello is produced by calling Class.Write (see previous post). I'd like to put hello (what the write() do) in the place of 'HI'; i don't know how to do this...thanks
    Regards

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    Quote Originally Posted by mickey
    hello is produced by calling Class.Write (see previous post). I'd like to put hello (what the write() do) in the place of 'HI'
    OK, so first you must capture the output generated by Class.Write.

    Something like:
    Qt Code:
    1. QString text;
    2. Class.Write( QTextStream( &text, IO_WriteOnly ) );
    To copy to clipboard, switch view to plain text mode 
    should be enough.

    And then:
    Qt Code:
    1. QDomElement tag1( doc.createElement( "PLAIN Property" ) );
    2. ...
    3. QDomText t1( doc.createTextNode( text ) );
    4. tag1.appendChild( t1 );
    To copy to clipboard, switch view to plain text mode 

    PS. AFAIK "PLAIN Property" is not a valid name of an element in XML.

  13. #13
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: xstrm.read();

    Hi, without repeat all code before with your solution I've got the prevoius problem inside the file:
    Qt Code:
    1. &lt;Light0 pos=&quot;4 1 4 1&quot; //wrong
    To copy to clipboard, switch view to plain text mode 
    But if I use my prevoius code works (but i can put what Write() do only before tag PLAIN and not inside it....
    Qt Code:
    1. ..............................
    2. Write(QTextStream(&f));
    3. QDomText t1 = doc.createTextNode("hello");
    4. .............
    To copy to clipboard, switch view to plain text mode 
    Regards

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: xstrm.read();

    Quote Originally Posted by mickey
    &lt;Light0 pos=&quot;4 1 4 1&quot; //wrong
    Because you add this as text. That's how a text which contains <, > and " looks like in XML.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.