PDA

View Full Version : Read&Writee in file



cebin
7th February 2011, 11:47
i want to copy the contents of one file to another file.But using the following code only first line is copied;


QFile f("/root/Desktop/n.txt");
f.open(QIODevice::ReadOnly | QIODevice::Text);
QFile f1("nn.txt");
f1.open(QIODevice::WriteOnly | QIODevice::Text);

QTextStream in(&f);
QTextStream out(&f1);

while(!f.atEnd())
{
in>>q;
out<<q;

}
f.close();
f1.close();

high_flyer
7th February 2011, 11:59
Why don't you use QFile::copy?

how is 'q' defined?

nighil
7th February 2011, 12:15
q is of type QString.i want to copy line by line,so that i can get some details seperated from the file.

cebin
7th February 2011, 12:22
q is of type QString.i want to copy line by line,so that i can get some details seperated from the file.

Lesiok
7th February 2011, 12:40
1. Operator >> reads word not line.
2. Use QTextStream::readline.

cebin
7th February 2011, 12:55
@Leisok: thanks... i also wanted to know how to write the data line by line..

squidge
7th February 2011, 13:28
Did you try using operator << ?

Lesiok
7th February 2011, 15:24
@Leisok: thanks... i also wanted to know how to write the data line by line..


out << q << '\n';