读文件
QString fileName = "D:\Lib\1.txt";QString str;QFile file(fileName);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::warning(this,"Warnning","can't open",QMessageBox::Yes);
}
QTextStream in(&file);str=in.readLine();//读取一行//如果读取多行
QString str2[MAX];
for(int 1=0 ;i<MAX; i++)
str2[i]=in.readLine();
file.close();
写文件
QString fileName=“D:/Lib/1.txt");
QString str="CSDN";
QFile file(fileName);
if(!file.open(QIODevice::ReadWrite | QIODevice::Text))
{
QMessageBox::warning(this,"sdf","can't open",QMessageBox::Yes);
}
QTextStream in(&file);
in<<str<<"\n";//如果写多行,同上循环即可
file.close
//删除文件
QString fileName=“D:/Lib/1.txt");
QFile file;
file.remove(fileName);
//关于文件名去扩展名等问题
QString fileName=“D:/Lib/1.txt");
QFile file;
file.remove(fileName); //关于文件名去扩展名等问题
QString fileName="1.txt";
int index = fileName.lastIndexOf(".");
fileName.truncate(index);
QMessageBox::warning(this,"Tips",fileName,QMessageBox::Yes);//现在的fileName就是"1"
//同理,lastIndexOf是找最后一个符合条件的位置,将文件名按字符串来处理就好了
//添加路径和扩展名
QString fienName = "1";
fileName.prepend("D:\Lib\");
fileName.append(".txt");
QMessageBox::warning(this,"Tips",fileName,QMessageBox::Yes);//fileName 为"D:\Lib\1.txt"//总之是对QString的简单操作而已