常见文件读写操作(XML、INI、TXT)

解析XML

// 解析XML
void GetXmlContents(string xmlPath, _Config& conf)
{
	tinyxml2::XMLDocument  docXml;
	XMLError errXml = docXml.LoadFile(xmlPath.data());		//加载USBConfig.xml
	if (XML_SUCCESS == errXml)
	{
		XMLElement* elmtRoot = docXml.RootElement();		//获取根元素

		// Get Config
		XMLElement* elmtDevList = elmtRoot->FirstChildElement("Device");
			//遍历 Device子节点
		for (XMLElement* elmtDev = elmtDevList->FirstChildElement(); elmtDev; elmtDev = elmtDev->NextSiblingElement())
		{
			string name = elmtDev->Name();
			string usb2Path = elmtDev->Attribute("USB2Addr");		// Attribute选定属性
			string usb3Path = elmtDev->Attribute("USB3Addr");

			if (usb2Path != "")   // 定位(选定)所有usb2Path
			{
				conf.usb2Path[name] = usb2Path;
			}

			if (usb3Path != "")
			{
				conf.usb3Path[name] = usb3Path;
			}
		}

		// Get Test Info
		XMLElement* elmtTest = elmtRoot->FirstChildElement("TEST");	//获取Test子节点

		// Get Log Path
		XMLElement* elmtLog = elmtTest->FirstChildElement("LOG");
		string::size_type ipos = (xmlPath.find_last_of("\\") + 1) == 0 ? xmlPath.find_last_of("/") + 1 : xmlPath.find_last_of("\\") + 1; //把xml中获取的路径中的单杠转为双杠
		string path1 = xmlPath.substr(ipos, xmlPath.length() - ipos);		// substr获取子串
		string path2 = path1.substr(0, path1.rfind("."));
		conf.logPath = elmtLog->Attribute("Path") + path2 + ".log";

		conf.head.location = elmtLog->Attribute("LOCATION");	// Attribute选定属性
		conf.head.caption = elmtLog->Attribute("CAPTION");

		vector<string> testList = { "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2" };
		for (auto it : testList)		// 遍历容器testList
		{
			XMLElement* elmtItem = elmtTest->FirstChildElement(it.data());	// 获取 Test下面的 "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2"
			_ItemConfig item;
			string errorcode = elmtItem->Attribute("ERRORCODE");	//选定属性ERRORCODE
			item.errorCode = errorcode;
			for (XMLElement* elmtTmp = elmtItem->FirstChildElement(); elmtTmp; elmtTmp = elmtTmp->NextSiblingElement())	// 遍历了 <USB_LAN2_1/>、  <USB_LAN2_2/>、  <USB_2.0_2/> 。。。。
			{
				string name = elmtTmp->Name();	// 获取
				item.usbPort[name] = "-";
				if ((it == "USB1.0" || it == "USB2.0") && conf.usb2Path.find(name) != conf.usb2Path.cend())
				{
					item.usbPort[name] = conf.usb2Path[name];
				}
				if ((it == "USB3.0" || it == "LOOPBACK" || it == "USB3_GEN2") && conf.usb3Path.find(name) != conf.usb3Path.cend())
				{
					item.usbPort[name] = conf.usb3Path[name];
				}
			}
			conf.item[it] = item;
		}
	}
}

读取INI

void GetINIContents(string iniPath, _Config& conf)
{
	char logPath[MAX_PATH] = { 0 }, location[MAX_PATH] = { 0 }, caption[MAX_PATH] = { 0 };
	GetPrivateProfileString("LOG_PATH", "PATH", "D:\\Bigdata\\INI\\", logPath, MAX_PATH, iniPath.c_str());
	GetPrivateProfileString("LOG_PATH", "LOCATION", "USB Port Test", location, MAX_PATH, iniPath.c_str());
	GetPrivateProfileString("LOG_PATH", "CAPTION", "USB Port", caption, MAX_PATH, iniPath.c_str());

	string::size_type ipos = (iniPath.find_last_of("\\") + 1) == 0 ? iniPath.find_last_of("/") + 1 : iniPath.find_last_of("\\") + 1;
	string path1 = iniPath.substr(ipos, iniPath.length() - ipos);
	string path2 = path1.substr(0, path1.rfind("."));
	conf.logPath = logPath + path2 + ".log";
	conf.head.location = location;
	conf.head.caption = caption;
	//printf("LOG:%s\n", conf.logPath.c_str());

	vector<string> testList = { "USB1.0", "USB2.0", "USB3.0", "LOOPBACK", "USB3_GEN2" };
	for (auto it : testList)
	{
		_ItemConfig itemConf;
		char allUSB[MAX_PATH] = { 0 };
		int len = GetPrivateProfileString(it.c_str(), NULL, "", allUSB, MAX_PATH, iniPath.c_str());
		for (int i = 0; i < len; i += strlen(allUSB + i) + 1)
		{
			string usbName = allUSB + i;
			char path[MAX_PATH] = { 0 };
			GetPrivateProfileString(it.c_str(), usbName.c_str(), "NULL", path, MAX_PATH, iniPath.c_str());
			if (!_stricmp(usbName.c_str(), "ERRORCODE"))
			{
				itemConf.errorCode = path;
			}
			else if (_stricmp(path, "NULL"))
			{
				itemConf.usbPort[usbName] = path;
			}
		}
		conf.item[it] = itemConf;
	}
}

读取TXT(指定位置读取操作)


	char ch[150];
	char* i;
	int line = 0, line1 = 0char str1[150] = "0-2-4-0";//SATA0
	
	FILE* fp = NULL;
	fp = fopen("./SATA.txt", "r");
	if (fp == NULL) {
		printf("can not find the SATA.txt");
		exit(-1);
	}

while (!feof(fp))
 {
		if (fgets(ch, sizeof(ch), fp) != NULL);
		{
			line++;
			i = strstr(ch, str1); //获取子串
			if (i) {
				line1 = line;
				strcpy(sataact[0].ID ,"3");
			}
			
			// if选定位置;strsrt()获取子串;
			if ((line == line1 + 3) & (line != 3)) {
				i = strstr(ch, "1.5");
				if (i) { strcpy(sataact[0].Speed, "1.5"); }
				i = strstr(ch, "3");
				if (i) { strcpy(sataact[0].Speed, "3.0"); }
				i = strstr(ch, "6");
				if (i) { strcpy(sataact[0].Speed, "6.0"); }
			}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QSettings 是 Qt 框架中提供的一个简单的读写配置文件的类。它可以用来读写不同格式的配置文件,包括 iniXML、注册表等等。在这里,我们介绍一下如何使用 QSettings 类读写 ini 文件。 在使用 QSettings 类读写 ini 文件之前,需要先创建一个 QSettings 对象,并指定要读写的配置文件的路径和格式。在本例中,我们假设配置文件的路径为 config.ini,格式为 ini。代码如下: ```cpp QSettings settings("config.ini", QSettings::IniFormat); ``` 接下来,我们可以使用 setValue() 和 value() 函数来写入和读取配置项。例如,我们可以将一个字符串写入配置文件中: ```cpp settings.setValue("name", "John"); ``` 接着,我们可以使用 value() 函数来读取这个配置项: ```cpp QString name = settings.value("name").toString(); ``` 如果配置项不存在,value() 函数会返回一个空值,因此我们需要使用默认值来避免程序出错。例如,我们可以使用下面的代码来读取一个整数配置项,如果配置项不存在,则返回默认值 100: ```cpp int value = settings.value("number", 100).toInt(); ``` 最后,需要注意的是,在使用完 QSettings 对象后,应该调用 sync() 函数将数据写入到文件中: ```cpp settings.sync(); ``` 完整的读写 ini 文件的示例代码如下: ```cpp #include <QSettings> int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // 创建 QSettings 对象 QSettings settings("config.ini", QSettings::IniFormat); // 写入配置项 settings.setValue("name", "John"); settings.setValue("age", 30); // 读取配置项 QString name = settings.value("name").toString(); int age = settings.value("age", 0).toInt(); // 输出配置项 qDebug() << "Name:" << name; qDebug() << "Age:" << age; // 同步数据到文件中 settings.sync(); return app.exec(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值