PDA

View Full Version : xstrm.read();



mickey
2nd July 2006, 18:34
Hi, Do anyone know why this instruction gets an runtime error and a console error like as
QFile::getch: Read operation not permitted ? Thanks


QString plainText = xstrm.read();


I need to do this below but I've got some problems....


QString plainText = xstrm.readline();
//cout << "plainText " << plainText << endl;
QDomText t1 = doc.createTextNode(plainText);
tag1.appendChild(t1);

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...

jacek
2nd July 2006, 19:23
What is that "xstrm" variable?

mickey
2nd July 2006, 22:27
QFile f(file);
f.open(IO_WriteOnly);
QTextStream xstrm(&f);

jacek
2nd July 2006, 22:39
QFile::getch: Read operation not permitted ?
...
f.open(IO_WriteOnly);
Do you see where the problem is?

mickey
2nd July 2006, 23:23
yes it was the error; but if I use .read() my app doesn't respond....
I coded this also:


QFile f(file);
f.open(IO_ReadOnly | IO_WriteOnly);
QTextStream xstrm(&f);
Class.Write(QTextStream(&f));
QString plainText = xstrm.readLine(); // or .read()
QDomText t1 = doc.createTextNode(plainText);
//QDomText t1 = doc.createTextNode("hello"); //with this ok
tag1.appendChild(t1);



void Class::Write(QTextStream &xstrm) {
xstrm << "hello\n";
}

after this in my file I found:


hello
a line of ascii code

why? "hello" is right...with "QDomText t1 = doc.createTextNode(plainText)" I'm trying to insert again "hello" in the file.....thanks

jacek
2nd July 2006, 23:49
I guess that QTextStream just reads the file from the current position, try adding "f.reset()" before xstrm.readLine().

mickey
3rd July 2006, 10:39
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..


&lt;Light0 pos=&quot;4 1 4 1&quot; //wrong



<Light0 pos="4 1 4 1" //right

Why this? thanks

jacek
3rd July 2006, 16:00
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?

mickey
3rd July 2006, 17:59
yes it was the error; but if I use .read() my app doesn't respond....
I coded this also:


QFile f(file);
f.open(IO_ReadOnly | IO_WriteOnly);
QTextStream xstrm(&f);
QDomElement tag1 = doc.createElement ("PLAIN"); // SEE THIS!!!!
Class.Write(QTextStream(&f));
QString plainText = xstrm.readLine(); // or .read()
QDomText t1 = doc.createTextNode(plainText);
//QDomText t1 = doc.createTextNode("hello"); //with this ok
tag1.appendChild(t1);



void Class::Write(QTextStream &xstrm) {
xstrm << "hello\n";
}

after this in my file I found:


hello
a line of ascii code


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

QDomText t1 = doc.createTextNode("hello");

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


QDomElement tag1 = doc.createElement ("PLAIN");
Class.Write(QTextStream(&f));
QDomText t1 = doc.createTextNode(..............);

content of strm between tag PLAIN. How this?

jacek
3rd July 2006, 19:43
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?

mickey
3rd July 2006, 20:00
ok I'll reformulate my problem:



QFile f(file);
f.open(IO_WriteOnly);
QTextStream xstrm(&f);
QDomDocument doc(file);
QDomElement root = doc.createElement (base_file);
doc.appendChild (root);
QDomElement tag1 = doc.createElement ("PLAIN Property");
root.appendChild(tag1);
scene.p.XMLWrite(QTextStream(&f));
Class.Write(QTextStream(&f));
QDomText t1 = doc.createTextNode("text");
QString xml = doc.toString();
xstrm << xml;

this produce a file:


hello
<!DOCTYPE level.xml>
<le>
<PLAIN Property> HI </PLAIN Property>
</le>

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

jacek
3rd July 2006, 20:48
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:
QString text;
Class.Write( QTextStream( &text, IO_WriteOnly ) );should be enough.

And then:
QDomElement tag1( doc.createElement( "PLAIN Property" ) );
...
QDomText t1( doc.createTextNode( text ) );
tag1.appendChild( t1 );

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

mickey
3rd July 2006, 21:38
Hi, without repeat all code before with your solution I've got the prevoius problem inside the file:


&lt;Light0 pos=&quot;4 1 4 1&quot; //wrong

But if I use my prevoius code works (but i can put what Write() do only before tag PLAIN and not inside it....


..............................
Write(QTextStream(&f));
QDomText t1 = doc.createTextNode("hello");
.............

jacek
3rd July 2006, 21:49
&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.

mickey
3rd July 2006, 22:06
sorry, but is it possible avoid this??

jacek
3rd July 2006, 22:15
sorry, but is it possible avoid this??
It depends on what you are trying to achieve. Does Class.Write() output XML or plain text?

What do you need QDomDocument for? Just to place the Class.Write() output between tags?

mickey
4th July 2006, 11:03
It depends on what you are trying to achieve. Does Class.Write() output XML or plain text?
What do you need QDomDocument for? Just to place the Class.Write() output between tags?
I'm a bit confused as you know. I need put output class between tag...
class write do this....insert text in xstrm...


void class::Write(QTextStream& xstrm) {
xstrm << "dimX=\"" << dimX << "\"dim......

I dont' undesrtand

jacek
4th July 2006, 16:09
I need put output class between tag...
Then maybe this will be enough?

QTextStream xstrm( &f );
...
xstrm << "<tag1>";
scene.p.XMLWrite( xstrm );
xstrm << "</tag1>";

xstrm << "<tag2>";
Class.Write( xstrm );
xstrm << "</tag2>";
...

mickey
4th July 2006, 17:33
I know this...I'm trying a way like this:


QDomElement tag1( doc.createElement( "PLAIN Property" ) );
QDomText t1( doc.createTextNode( text ) );
tag1.appendChild( t1 );

using QDomElement to insert tag....