亲测可用,自用分享
//获取根路径
QString runPath = QCoreApplication::applicationDirPath();//获取项目的根路径
QString file_name = QFileDialog::getOpenFileName(this, QStringLiteral("选择文件"), runPath, "Text Files(*.dat)", nullptr, QFileDialog::DontResolveSymlinks);
QFile myFile(file_name);
//写文件
QFile myFile_write("C:/Users/lenovo/Documents/2.dat");
if (!myFile_write.open(QIODevice::WriteOnly))
{
qDebug() << myFile_write.errorString();
}
//写文件---可以用作另存
QTextStream textStream_write(&myFile_write);
textStream_write << str1;
textStream_write << ",";
textStream_write << str2;
textStream_write << " __ ";
textStream_write << count_line;
textStream_write << "\n";
textStream_write.flush();
//读取文件
if (!myFile.open(QIODevice::ReadOnly))
{
qDebug() << myFile.errorString();
}
QTextStream textStream(&myFile);
textStream.setDevice(&myFile);
int count_line = 0; //文件行数
QVector<double> point_x, point_y; //x,y容器
while (!textStream.atEnd())
{
QString str = textStream.readLine();
QString str_first = *str.begin();
if (str_first =="%")
{
continue;
}
double str1 = str.split(",",QString::SkipEmptyParts)[0].toDouble();
double str2 = str.split(",",QString::SkipEmptyParts)[1].toDouble();
point_x.push_back(str1);
point_y.push_back(str2);
count_line++;
}
textStream.seek(0);
myFile_write.close();