windows 如何使用资源文件(txt - 制表符分隔)作为 win32 应用程序的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1994576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use a resource file (txt - tab delimited) as a data source for win32 application
提问by coder
i am working on a win32 app. i am currently using the text file which has tab delimited data as a source. i want to embed this as a resource so that i need not worry about shipping this file along with .exe file.
我正在开发一个 win32 应用程序。我目前正在使用具有制表符分隔数据的文本文件作为源。我想将此作为资源嵌入,这样我就不必担心将此文件与 .exe 文件一起传送。
can anyone tell me how to do it?
谁能告诉我怎么做?
回答by alemjerus
Windows binary can have resources embedded in them. Most resources are of predetermined type (e.g. a menu, an icon or a bitmap) but you can also embed arbitrary binary data (e.g. a text file). The proper syntax is hard to figure out just from reading msdn docs.
Windows 二进制文件中可以嵌入资源。大多数资源都是预先确定的类型(例如菜单、图标或位图),但您也可以嵌入任意二进制数据(例如文本文件)。仅通过阅读 msdn 文档很难找出正确的语法。
This snippet shows how to embed a binary resource from a file.
此代码段展示了如何从文件中嵌入二进制资源。
First you need to define a resource identifier in a header file (e.g. resource.h) that will be used by both C compiler and resource compiler:
首先,您需要在 C 编译器和资源编译器都使用的头文件(例如,resource.h)中定义一个资源标识符:
#define MY_RESOURCE 300
Then you need to add to your resource file (e.g. resource.rc):
然后你需要添加到你的资源文件(例如resource.rc)中:
MY_RESOURCE RCDATA "file-with-data.txt"
And finally, this is how you can get to this data:
最后,这是获取这些数据的方法:
void WorkOnResource(void) { HGLOBAL res_handle = NULL; HRSRC res; char * res_data; DWORD res_size; // NOTE: providing g_hInstance is important, NULL might not work res = FindResource(g_hInstance, MAKEINTRESOURCE(MY_RESOURCE), RT_RCDATA); if (!res) return; res_handle = LoadResource(NULL, res); if (!res_handle) return; res_data = (char*)LockResource(res_handle); res_size = SizeofResource(NULL, res); /* you can now use the resource data */ }
回答by Hans Passant
Define the resource ID, add this to the .rc file:
定义资源 ID,将其添加到 .rc 文件中:
ID_CUSTOM1 ANYTHINGGOESHERE "filename.txt"
Read it at runtime with code like this:
在运行时使用如下代码读取它:
HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(ID_CUSTOM1), L"ANYTHINGGOESHERE");
HGLOBAL hMem = LoadResource(hInst, hRes);
DWORD size = SizeofResource(hInst, hRes);
char* resText = (char*)LockResource(hMem);
char* text = (char*)malloc(size + 1);
memcpy(text, resText, size);
text[size] = 0;
FreeResource(hMem);
// use text...
回答by Indy9000
why not use a header file and put all your data in a static array. That way you don't have to parse a text file or worry about deployment
为什么不使用头文件并将所有数据放在静态数组中。这样您就不必解析文本文件或担心部署
回答by ta.speot.is
If you are looking for the "correct" way to do this then I would suggest adding your text file as a resource (as a string table or binary) and using LoadString or FindResource to access it.
如果您正在寻找执行此操作的“正确”方法,那么我建议将您的文本文件添加为资源(作为字符串表或二进制文件)并使用 LoadString 或 FindResource 来访问它。
回答by stijn
in Visual Studio, you can add text as a resource just like any other resource.
在 Visual Studio 中,您可以像添加任何其他资源一样将文本添加为资源。
In the resource.rc file of your project:
在项目的 resource.rc 文件中:
IDR_MYRESOURCE MYCUSTOMRESOURCETYPE "path_to_file.txt"
in the resource.h file:
在 resource.h 文件中:
#define IDR_MYRESOURCE 104
(or you can add these through the resource editor by selecting "Add Resource", then "New")
(或者您可以通过资源编辑器通过选择“添加资源”,然后“新建”来添加这些)
To load the resource in code:
在代码中加载资源:
HRSRC hRes = FindResource( 0, "#104", "MYCUSTOMRESOURCETYPE" );
HGLOBAL hData = LoadResource( 0, hRes );
LPVOID data = LockResource( hData );
Now data points to the text, and can be casted to string.
现在数据指向文本,并且可以转换为字符串。
edithmm looks like everyone was posting the same answer at the same time ;P
编辑嗯看起来每个人都在同一时间发布相同的答案;P