QT控制台程序,如果需要匹配的字符串在首行的话,无法完成替换。麻烦各位帮忙看看。打印出来的debug显示已经完成替换了,但是首行无法写入。其他行就完全没问题。。。。。。QT环境是QT5.11.0.。。。。谢谢
#include <QtCore/QCoreApplication>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>
QString str,str2;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream cin(stdin, QIODevice::ReadOnly);
QTextStream cout(stdout, QIODevice::WriteOnly);
QString strAll;
QStringList strList;
QFile readFile("F:/test.txt");
if(readFile.open((QIODevice::ReadOnly|QIODevice::Text)))
{
QTextStream stream(&readFile);
strAll=stream.readAll();
}readFile.close();
QFile writeFile("F:/test.txt");
if(writeFile.open(QIODevice::WriteOnly|QIODevice::Text))
{
QTextStream stream(&writeFile);
strList=strAll.split("\n");
cout<<"input string"<<endl;
cin>>str;
cout<<"input string to replace"<<endl;
cin>>str2;
for(int i=0;i<strList.count();i++)
{
if(i==strList.count()-1)
{
//最后一行不需要换行
stream<<strList.at(i);
}
else
{
stream<<strList.at(i)<<'\n';
}
//cout<<strList.at(i)<<endl;
strList.replaceInStrings(str,str2);
#if 0
if(strList.at(i).contains(str))
{
//QString tempStr=strList.at(i);
strList.replaceInStrings(str,str2);
cout<<strList.at(i)<<endl;
stream<<'\n';
i++;
}
#endif
}
}
writeFile.close();
}