我有一个使用sqlite的C ++程序。 我想将SQL查询存储在一个单独的文件中 – 一个纯文本文件, 而不是一个源代码文件 – 但是将该文件embedded到可执行文件中,就像资源一样。
(这个必须在Linux上运行,所以我不能把它作为一个实际的资源存储,据我所知,尽pipe如果它是Windows的话,那将是完美的)。
有没有简单的方法来做到这一点,还是会有效地要求我为Linux编写自己的资源系统? (很容易,但需要更长的时间。)
与Windows上的标准USB PTP / MTP相机进行通信的本地方式是什么?
在Linux上testingnetworking代码的最准确的方法是什么?
在WinApi中如何发送variables到属于另一个进程/线程的对话框
使用Visual Studio 2013 Express为Windows XP构build错误
将GetVersionEx数字转换为操作系统名称
ASCII英文文本/ ASCII C程序文本问题
如何将拼写检查程序添加到Richtext框?
使用rundll32.exedebugging本机DLL,无法加载符号
如何正确使用_syscall3
如何读取和写入Windows“networking位置”
您可以使用objcopy将文件的内容绑定到程序可以使用的符号上。 例如,参见这里获取更多信息。
您可以随时编写一个小程序或脚本来将您的文本文件转换为头文件,并将其作为构建过程的一部分运行。
使用宏。 从技术上讲,该文件将是源代码文件,但它不会像这样。 例:
//queries.incl - sql queries Q(SELECT * FROM Users) Q(INSERT [a] INTO Accounts) //source.cpp #define Q(query) #query,char * queries[] = { #include "queries.incl" }; #undef Q
之后你可以用同一个文件对这个文件进行各种其他的处理,比如说你想要有一个数组和一个散列图,你可以重新定义Q来完成另一个工作并完成它。
以下是我们用于跨平台嵌入文件的示例。 这很简单,但可能会为你工作。
您可能还需要更改在escapeLine函数中处理换行符的方式。
#include <string> #include <iostream> #include <fstream> #include <cstdio> using namespace std; std::string escapeLine( std::string orig ) { string retme; for (unsigned int i=0; i<orig.size(); i++) { switch (orig[i]) { case '\': retme += "\\"; break; case '"': retme += "\""; break; case 'n': // Strip out the final lineFeed. break; default: retme += orig[i]; } } retme += "\n"; // Add an escaped lineFeed to the escaped string. return retme; } int main( int argc,char ** argv ) { string filenamein,filenameout; if ( argc > 1 ) filenamein = argv[ 1 ]; else { // Not enough arguments fprintf( stderr,"Usage: %s <file to convert.mel> [ <output file name.mel> ]n",argv[0] ); exit( -1 ); } if ( argc > 2 ) filenameout = argv[ 2 ]; else { string new_ending = "_mel.h"; filenameout = filenamein; std::string::size_type pos; pos = filenameout.find( ".mel" ); if (pos == std::string::npos) filenameout += new_ending; else filenameout.replace( pos,new_ending.size(),new_ending ); } printf( "Converting "%s" to "%s"n",filenamein.c_str(),filenameout.c_str() ); ifstream filein( filenamein.c_str(),ios::in ); ofstream fileout( filenameout.c_str(),ios::out ); if (!filein.good()) { fprintf( stderr,"Unable to open input file %sn",filenamein.c_str() ); exit( -2 ); } if (!fileout.good()) { fprintf( stderr,"Unable to open output file %sn",filenameout.c_str() ); exit( -3 ); } // Write the file. fileout << "tempstr = "; while( filein.good() ) { string buff; if ( getline( filein,buff ) ) { fileout << """ << escapeLine( buff ) << """ << endl; } } fileout << ";" << endl; filein.close(); fileout.close(); return 0; }
这有点丑,但你总是可以使用像这样的东西:
const char * query_foo =
#include“query_foo.txt”
const char * query_bar =
#include“query_bar.txt”
其中query_foo.txt将包含带引号的查询文本。
我已经看到这是通过将资源文件转换为C源文件来完成的,只有一个字符数组定义为包含十六进制格式的资源文件的内容(以避免恶意字符的问题)。 这个自动生成的源文件然后被简单地编译并链接到项目。
实现转换器应该很容易为每个资源文件转储C文件,也可以写一些Facade函数来访问资源。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。