WritePrivateProfileString和GetPrivateProfileString最大长度测试
程序员文章站
2022-03-13 23:52:44
...
1. 前言
- 读写Ini文件代码如下
Ini.h
#ifndef __INI__
#define __INI__
#include <tchar.h>
#include <Windows.h>
#include <string>
class Ini
{
public:
static BOOL WriteIni(
std::wstring lpNodeName, // INI文件中的一个字段名[节名]可以有很多个节名
std::wstring lpKeyName, // lpNodeName 下的一个键名,也就是里面具体的变量名
std::wstring lpValue, // 键值,也就是数据
std::wstring lpFileName // INI文件的路径
);
static DWORD ReadStringFromIni(
LPCTSTR lpAppName, // 节名
LPCTSTR lpKeyName, // 键名,读取该键的值
LPCTSTR lpDefault, // 若指定的键不存在,该值作为读取的默认值
LPTSTR lpReturnedString, // 一个指向缓冲区的指针,接收读取的字符串
DWORD nSize, // 指定lpReturnedString指向的缓冲区的大小
LPCTSTR lpFileName
);
static UINT ReadIntFromIni(
LPCTSTR lpAppName, // 同上
LPCTSTR lpKeyName, // 同上
INT nDefault, // 若指定的键名不存在,该值作为读取的默认值
LPCTSTR lpFileName
);
private:
Ini() {};
Ini(const Ini &ini) {};
~Ini(){};
};
#endif
Ini.cpp
#include "Ini.h"
BOOL Ini::WriteIni(
std::wstring lpNodeName,
std::wstring lpKeyName,
std::wstring lpValue,
std::wstring lpFileName
)
{
return WritePrivateProfileString(lpNodeName.c_str(), lpKeyName.c_str(), lpValue.c_str(), lpFileName.c_str());
}
DWORD Ini::ReadStringFromIni(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
)
{
return GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName);
}
UINT Ini::ReadIntFromIni(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
INT nDefault,
LPCTSTR lpFileName
)
{
return GetPrivateProfileInt(lpAppName, lpKeyName, nDefault, lpFileName);
}
2. 测试
测试值的最大长度
- 测试单次写入键value的值长度为65000个字符
- 结果表明可以成功的写入65000个字符
测试的代码
#include <Windows.h>
#include <tchar.h>
#include "Ini.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, TCHAR const *argv[])
{
TCHAR tmp[65000];
wstring s(65000, L'.');
wcout << s.length() << endl;
Ini::WriteIni(L"test.txt", L"value", s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", L"value", L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << s.length() << endl;
return 0;
}
- 但当单次写入的长度大于等于66000个字符时
- 第一次是成功写入了66000个字符,但读出只有464个字符的长度
- 且多次运行的话,value这个键的值的字符不是被覆盖,而是一直增加(65000个字符的情况下,重新写入值时,旧的值会被覆盖,不会增加)
- 运行三次的情况如下(结果显示写入并不是3*66000的长度)
测试的代码
#include <Windows.h>
#include <tchar.h>
#include "Ini.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, TCHAR const *argv[])
{
TCHAR tmp[65000];
wstring s(65000, L'.');
wcout << s.length() << endl;
Ini::WriteIni(L"test.txt", L"value", s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", L"value", L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << s.length() << endl;
return 0;
}
- 写入多个键的值长度为65000个字符
- 结果如图,表明键的长度能够达到65000个字符
#include <Windows.h>
#include <tchar.h>
#include "Ini.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, TCHAR const *argv[])
{
TCHAR tmp[65000];
wstring s(65000, L'.');
wcout << L"s length: "<< s.length() << endl;
Ini::WriteIni(L"test.txt", L"value1", s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", L"value1", L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value1 length: " << s.length() << endl;
Ini::WriteIni(L"test.txt", L"value2", s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", L"value2", L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value2 length: " << s.length() << endl;
Ini::WriteIni(L"test.txt", L"value3", s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", L"value3", L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value3 length: " << s.length() << endl;
Ini::WriteIni(L"test.txt", L"value4", s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", L"value4", L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value4 length: " << s.length() << endl;
return 0;
}
测试键的最大长度
- 有了上述的经验
- 直接测试多个键的长度为65000个字符(结果表明也是正常的)
测试代码:
#include <Windows.h>
#include <tchar.h>
#include "Ini.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, TCHAR const *argv[])
{
TCHAR tmp[65000];
wstring s1(65000, L'1');
wcout << L"s1 length: "<< s1.length() << endl;
Ini::WriteIni(L"test.txt", s1, L"value1", L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", s1.c_str(), L"", tmp, 65000, L".\\test.txt");
wcout << L"value1: " << tmp << endl;
wstring s2(65000, L'2');
wcout << L"s2 length: "<< s2.length() << endl;
Ini::WriteIni(L"test.txt", s2, L"value2", L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", s2.c_str(), L"", tmp, 65000, L".\\test.txt");
wcout << L"value2: " << tmp << endl;
wstring s3(65000, L'3');
wcout << L"s3 length: "<< s3.length() << endl;
Ini::WriteIni(L"test.txt", s3, L"value3", L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", s3.c_str(), L"", tmp, 65000, L".\\test.txt");
wcout << L"value3: " << tmp << endl;
return 0;
}
键与值长度均为65000个字符
- 结果显示也正常
#include <Windows.h>
#include <tchar.h>
#include "Ini.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, TCHAR const *argv[])
{
TCHAR tmp[65000];
wstring s(65000, L'.');
wstring s1(65000, L'1');
wcout << L"s1 length: "<< s1.length() << endl;
Ini::WriteIni(L"test.txt", s1, s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", s1.c_str(), L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value1 length: " << s.length() << endl;
wstring s2(65000, L'2');
wcout << L"s2 length: "<< s2.length() << endl;
Ini::WriteIni(L"test.txt", s2, s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", s2.c_str(), L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value2 length: " << s.length() << endl;
wstring s3(65000, L'3');
wcout << L"s3 length: "<< s3.length() << endl;
Ini::WriteIni(L"test.txt", s3, s, L".\\test.txt");
Ini::ReadStringFromIni(L"test.txt", s3.c_str(), L"", tmp, 65000, L".\\test.txt");
s = tmp;
wcout << L"value3 length: " << s.length() << endl;
return 0;
}
总结
-
WritePrivateProfileString
和GetPrivateProfileString
键与值的最大长度大概为 65000个字符, 精确值未测量 - 可能与编译器,操作系统有关; 上述结果使用的是vs2008, windows 7环境测试的
推荐阅读
-
(20)ASP.NET Core EF创建模型(必需属性和可选属性、最大长度、并发标记、阴影属性)
-
【C#】C# 读写ini配置文件 WritePrivateProfileString和GetPrivateProfileString
-
第五章 树——二叉树最大路径长度和最大带权路径和
-
求数组元素和是K的倍数的子串的最大长度
-
(20)ASP.NET Core EF创建模型(必需属性和可选属性、最大长度、并发标记、阴影属性)
-
iis最大连接数和队列长度_html/css_WEB-ITnose
-
iis最大连接数和队列长度 - jerrylsxu
-
iis最大连接数和队列长度 - jerrylsxu
-
iis最大连接数和队列长度_html/css_WEB-ITnose
-
WritePrivateProfileString和GetPrivateProfileString最大长度测试