QT文件读写
2014年9月14日
文件作为存储在物理介质上的数据,以各种方式编码,常需要读写操作。
1) 判断文件存在性
2) 读取文件
3) 写入文件
4) 删除文件
文件作为IO设备(直接继承自QIODevice)被QT进行读写操作。一般以QTextStream(字符串)和QDataStream(二进制)作为读写的流,也可以作为IO设备直接读写。
打工、关闭文件:QFile::open()、close()。
存在性、删除:QFile::exists()、QFile::remove()。
刷新:QFile::flush()。
大小:QFile::size()(需要先打开)。
读取文件:QIODevice::readLine(),read(),readAll()。
写入文件:QIODevice::write()。
按字符操作:QIODevice::getChar(),putChar(),unGetChar()。
当前位置:QFile::pos(),seek()。
结束标志:QFile::atEnd()。
注意:对于路径,使用/,不运行\。
//readfile
QStringstrFileName=lstArg[1];
QFilefCmdParam(strFileName);
if(!fCmdParam.exists()){//checkifexist
qDebug("%sisnotexist.\n",strFileName);
returnQStringList();
}
fCmdParam.open(QIODevice::ReadOnly);
QStringListlstCmdParam;
while(!fCmdParam.atEnd()){
QByteArraybaCmdParam=fCmdParam.readLine();
QStringstrCmdParam(baCmdParam);
//multilines
strCmdParam.remove("\r\n");
strCmdParam=strCmdParam.trimmed();
strCmdParam.remove("\\");
if(strCmdParam.startsWith('\"')||strCmdParam.endsWith('\"')){
strCmdParam=strCmdParam.mid(1,strCmdParam.length()-2);
}
qDebug()<<"strCmdParam="<<strCmdParam<<endl;
lstCmdParam<<strCmdParam;
http://qimo601.iteye.com/blog/1470944