1.获取多个文件路径+名字
#include <QFileDialog>
void MainWindow::on_pushButton_clicked()
{
QString dialog[3],name[4];
QStringList split[3];
QFileDialog add_file_dialog;
add_file_dialog.setFileMode(QFileDialog::ExistingFiles);
QStringList filename=add_file_dialog.getOpenFileNames(
this, tr("open txt"),".", tr("select(*.txt)"));
int i=0;
for(auto temp:filename){
dialog[i]=temp;
split[i] = dialog[i].split("/");
name[i] = split[i].last();
i++;
}
name[3] = QString("%1,%2,%3").arg(name[0]).arg(name[1]).arg(name[2]);
ui->label->setText(name[3]);
}
2.获取路径+新建名字
QString saveName;//保存名字
QString savePath;
QString filePath;//存储保存路径
QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
QString str = time.toString("mm:ss-yyyyMMdd"); //设置显示格式
filePath = QFileDialog::getExistingDirectory(this,"");//获取文件夹路径
if(!filePath.isEmpty())
{
saveName="savename";
savePath=QString("%1/%2-%3.txt").arg(filePath).arg(saveName).arg(str);
const std::string save = savePath.toStdString();
//todo
}
3.覆盖写入
std::ofstream outFile(savePath);//example:string savePath = "/a.txt"
for(int i=0;i<10;i++){outFile<<i<<" ";}
outFile.close();
3.读入
std::ifstream inFile(savePath);
if(inFile){
for(int i=0;i<8;i++){inFile>>parameter[i];}
inFile.close();
}