PDA

View Full Version : replace line of a file



masuk
1st February 2011, 04:27
Hi
I am newbie on Qt.

I have a file which contains these lines


First line
Second line
Third line


Is there any way to replace first line with a new line but no change in other lines,which should result in



This is line one
Second line
Third line


I am using qlineedit for write data in file.

can anyone please give code snippet.

Advance thanx.

ChrisW67
1st February 2011, 04:58
Nothing to do with Qt per se. General steps:

Open original file for reading
Open temporary file for writing
Write lines from original file until you reach the line you want to replace
Write the replacement line
Skip the line from the original file
Write lines from original file until end of file
Close temporary and original file
If all of above successful, remove original file and copy/rename temporary file.


Start with QFile, QTextStream and QTemporaryFile in the Qt docs. The temporary file could be in-memory; see QBuffer.

masuk
1st February 2011, 05:26
many many thanx ChrisW67 for your repl.

But if you dont mind can you please give the sample code/full code.

ChrisW67
1st February 2011, 08:00
No. Try it yourself.

masuk
1st February 2011, 08:06
hmm.......

ChrisW67
1st February 2011, 08:28
The thing is, this is not the QtCentre Write Your Program For You Service. People here are willing to help those with evidence of having tried to do it themselves. I have already given you a program flow and told you all the necessary components. To do any more would be me writing the program for you... IMHO you will not learn as much from the exercise.

masuk
1st February 2011, 17:49
I need the solution code

thanks

Lykurg
1st February 2011, 18:02
...and I people who try at least. And by the way from where else do you copy and past your application? What have you tried so far? What problems are you facing? Had you have a look at QFile, QTextStream and other Qt classes?

If you can't do it, hire someone you can and pay him money or start to learn to program.

kosasker
3rd February 2011, 13:05
QFile file(iplogini);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{ //you can send a message or throw error signal here
return;
}

QTextStream in(&file);
QStringList line;
while (!in.atEnd())
{
line.append(in.readLine());
}
QString lineedit =line.at(4);
lineedit.replace("StorageFolder=","Storage Folder Path"); //first arg is text that, which will
//gone a be change and second arg is text will replace the other one...

First open file, if file exist
second read file, line by line, and write each line to a string list element
third if your text file will not be change (my file is a static ini file) and if you now that which line you want to replace, we can get it from qstringlist elements with "at()" public function

so if i can get the line, which i wanted, i can write in another string, after that with replace function i can replace char to in.

Sorry for bad english!

hope this help

masuk
3rd February 2011, 19:36
many many thanks kosasker....:)

masuk
4th February 2011, 18:02
thanks to all....specially thanks ChrisW67 and kosasker..........:)

masuk
5th February 2011, 09:51
solution code:


void Form::replace()
{
QFile file("E:/workplace/file1.txt");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString line = in.readLine();
QString line1 = in.readLine()+"\n";
QString line2 = in.readLine()+"\n";
file.close();


QFile ofile("E:/workplace/file2.txt");
ofile.open(QIODevice::ReadWrite | QIODevice::Text);
QTextStream out(&ofile);
out << ui->lineEdit_2->text()+"\n";
out << line1;
out << line2;
ofile.close();

file.remove();
ofile.rename("E:/workplace/file2.txt","E:/workplace/file1.txt");

this->close();

}