QTextStream读写文件

本文介绍了使用QT库中的QTextStream进行文件读写的实现方式。包括如何将注册表键值写入文件及从文件中读取并解析这些键值。通过具体的代码示例展示了文件操作的具体步骤。

QT中的读和写文件的方式也有很多,这是我用到的写和读(QTextStream):

写:

[cpp]  view plain  copy
  1. /* 
  2.  *  将注册表子键值写进文件 
  3.  */  
  4. void RegistryWindow::writeIntoFileOfKeyValueTable(QString keyName,QString name,QString type,QString data)  
  5. {  
  6.     //创建文件夹  
  7.     if (!QDir::current().exists("keyValueTable"))  
  8.     {  
  9.         QDir::current().mkdir("keyValueTable");  
  10.     }  
  11.   
  12.   
  13.     QString re = keyName.replace(QRegExp("\\\\"),"@"); //正则表达式,@来代替分隔  
  14.   
  15.   
  16.     //创建文件  
  17.     QFile file("./keyValueTable/" + re +".txt");  
  18.   
  19.   
  20.     if(!file.open(QIODevice::WriteOnly|QIODevice::Append))  
  21.     {  
  22.         QMessageBox::information(this,QStringLiteral("打开文件失败!"),file.errorString());  
  23.     }  
  24.   
  25.   
  26.     QTextStream fileOut(&file);  
  27.   
  28.   
  29.     // fileOut.setCodec("UTF-8");  //unicode UTF-8  ANSI  
  30.   
  31.   
  32.     fileOut << name << "$" << type << "$" << data << "\n";  
  33.   
  34.   
  35.     file.flush();  
  36.   
  37.   
  38.     file.close();  
  39. }  

读:

[cpp]  view plain  copy
  1. /* 
  2.  *  将文件中的注册表键值读出,并显示到注册表键值表格上 
  3.  */  
  4. void RegistryWindow::readFromFileOfKeyValueTable(QString keyName)  
  5. {  
  6.     QString re = keyName.replace(QRegExp("\\\\"),"@");  
  7.   
  8.     //创建文件  
  9.     QFile file("./keyValueTable/" + re +".txt");  
  10.   
  11.     //打开文件  
  12.     if(!file.open(QIODevice::ReadOnly))  
  13.     {  
  14.         QMessageBox::information(this,QStringLiteral("打开文件失败!"),file.errorString());  
  15.     }  
  16.   
  17.     QTextStream fileIn(&file);  
  18.     QString str = fileIn.readLine(0);  
  19.     while (str.contains("$"))  
  20.     {  
  21.         qDebug() << str;  
  22.         QStringList strList = str.split("$");  
  23.   
  24.         QTextCodec *codec = QTextCodec::codecForLocale();//显示汉字的  
  25.   
  26.         //最终要存储的键值  
  27.         QIcon *resultValueIcon;  
  28.         QString resultValueName = strList.at(0);  
  29.         QString resultValueType = strList.at(1);  
  30.         QString resultValueData = strList.at(2);  
  31.   
  32.         //REG_DWORD类型的键值  
  33.         if (QString(resultValueType).contains("REG_DWORD"))  
  34.         {  
  35.             resultValueIcon = new QIcon(":/Images/Resources/reg_011110.png");  
  36.         }  
  37.         //REG_BINARY类型的键值  
  38.         else if (QString(resultValueType).contains("REG_BINARY"))  
  39.         {  
  40.             resultValueIcon = new QIcon(":/Images/Resources/reg_011110.png");  
  41.         }  
  42.         //REG_SZ\REG_EXPAND_SZ\REG_MULTI_SZ类型的键值  
  43.         else  
  44.         {  
  45.             resultValueIcon = new QIcon(":/Images/Resources/reg_ab.png");  
  46.         }  
  47.   
  48.         //键值名称  
  49.         QTableWidgetItem *itemName = new QTableWidgetItem;  
  50.         itemName->setIcon(*resultValueIcon);  
  51.         itemName->setText(resultValueName);  
  52.         //itemName->setText(codec->toUnicode(keyInfo.valueName));  
  53.   
  54.         //键值类型  
  55.         QTableWidgetItem *itemType = new QTableWidgetItem;  
  56.         itemType->setText(resultValueType);  
  57.   
  58.         //键值数据  
  59.         QTableWidgetItem *itemData = new QTableWidgetItem;  
  60.         itemData->setText(resultValueData);  
  61.   
  62.         myTableWidget->setRowCount(myTableWidget->rowCount()+1);  
  63.         myTableWidget->setRowHeight(myTableWidget->rowCount()-1,20);//设置行高  
  64.         myTableWidget->setItem(myTableWidget->rowCount()-1,0,itemName);  
  65.         myTableWidget->setItem(myTableWidget->rowCount()-1,1,itemType);  
  66.         myTableWidget->setItem(myTableWidget->rowCount()-1,2,itemData);  
  67.   
  68.         str = fileIn.readLine(0);  
  69.     }  
  70. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值