Write content from old file to new file in steps
-
Hi,
I want to write content from an old file to a new file in two steps:
Step 1: copy first 24 lines from old file to the new file as it is.
Step 2: Read line by line from 25 do some modification in each lines and write to the new fileI did as follows:
Old file opened in Read Only mode , copied contents to QTextStream
instream
Opened the file to be written in ReadWrite mode and there are twowhileloopsQString oldfilename = "log_file"; QString newfilename = "new_log_file"; QString path1 = QCoreApplication::applicationDirPath()+"/"+oldfilename +".txt"; QString path2 = QCoreApplication::applicationDirPath()+"/"+newfilename+".txt"; QFile readfile(path1); if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug() << "Error opening file: "<<readfile.errorString(); } QTextStream instream(& readfile);Next, I opened the file to be written in ReadWrite mode, with two
whileloops. The first while loop writes the first 24 lines into the new file, and that works.The problem is with the 2nd while loop , that is meant to read QTextStream
instreamfrom line 24 , line by line. I'veifstatements to go through each line and show it in theqDebug. But that not showing the right lines!I feel like it's problem I defined in the 2nd while loop but not sure what exactly to be done.
QFile writefile(path2); if(file.open(QIODevice::ReadWrite | QIODevice::Text)) { QTextStream outstream(& file); int linecount_1 = 0; while(linecount_1<24){ QString line = instream.readLine(); outstream <<line<< '\n'; linecount_1++; } // <-------upto this write on a new file int linecount_2 = 0 while(!instream.atEnd()) { QString line = instream.readLine(); if(nb_line == 24) { qDebug () << line ; // this is not showing the right line } if(nb_line == 25) { qDebug () << line ; } if(nb_line == 26 ) { qDebug () << line ; } ++linecount_2; } readfile.close(); writefile.close(); }can someone spot issues in the implementation?
-
Hi,
I want to write content from an old file to a new file in two steps:
Step 1: copy first 24 lines from old file to the new file as it is.
Step 2: Read line by line from 25 do some modification in each lines and write to the new fileI did as follows:
Old file opened in Read Only mode , copied contents to QTextStream
instream
Opened the file to be written in ReadWrite mode and there are twowhileloopsQString oldfilename = "log_file"; QString newfilename = "new_log_file"; QString path1 = QCoreApplication::applicationDirPath()+"/"+oldfilename +".txt"; QString path2 = QCoreApplication::applicationDirPath()+"/"+newfilename+".txt"; QFile readfile(path1); if(!readfile.open(QIODevice::ReadOnly | QIODevice::Text)){ qDebug() << "Error opening file: "<<readfile.errorString(); } QTextStream instream(& readfile);Next, I opened the file to be written in ReadWrite mode, with two
whileloops. The first while loop writes the first 24 lines into the new file, and that works.The problem is with the 2nd while loop , that is meant to read QTextStream
instreamfrom line 24 , line by line. I'veifstatements to go through each line and show it in theqDebug. But that not showing the right lines!I feel like it's problem I defined in the 2nd while loop but not sure what exactly to be done.
QFile writefile(path2); if(file.open(QIODevice::ReadWrite | QIODevice::Text)) { QTextStream outstream(& file); int linecount_1 = 0; while(linecount_1<24){ QString line = instream.readLine(); outstream <<line<< '\n'; linecount_1++; } // <-------upto this write on a new file int linecount_2 = 0 while(!instream.atEnd()) { QString line = instream.readLine(); if(nb_line == 24) { qDebug () << line ; // this is not showing the right line } if(nb_line == 25) { qDebug () << line ; } if(nb_line == 26 ) { qDebug () << line ; } ++linecount_2; } readfile.close(); writefile.close(); }can someone spot issues in the implementation?
@viniltc said in Write content from old file to new file in steps:
if(nb_line == 24)
{
qDebug () << line ; // this is not showing the right line
}
if(nb_line == 25)
{
qDebug () << line ;
}if(nb_line == 26 ) { qDebug () << line ; }I'm not sure what this is supposed to do?
You're not changing nb_line inside that while loop...
I also don't know why you need these checks at all: if you read 24 lines in the first while loop, then in the second you will read lines starting with line 25. -
@viniltc said in Write content from old file to new file in steps:
if(nb_line == 24)
{
qDebug () << line ; // this is not showing the right line
}
if(nb_line == 25)
{
qDebug () << line ;
}if(nb_line == 26 ) { qDebug () << line ; }I'm not sure what this is supposed to do?
You're not changing nb_line inside that while loop...
I also don't know why you need these checks at all: if you read 24 lines in the first while loop, then in the second you will read lines starting with line 25.