编译存储在外部文本文件中的数组(c++使用命令行g++编译)
Compiling arrays stored in external text files (C++ compiled using command line g++)
我是一个c++程序员新手,所以如果这是一个幼稚的问题,请原谅我。我有一些文件包含大型数组,其中包含我以前在javascript应用程序中使用过的数万个字符串。是否有一些方法将这些包含到c++源代码中,以便数组与代码一起编译?
目前,文件被格式化为返回(javascript)文字数组的函数,如下所示:
// javascript array stored in .js text file
function returnMyArray()
{
return ["string1", "string2", "string3", ... "stringBigNumber"];
} // eof returnMyArray()
我'包含'外部文件与通常的javascript脚本&SRC标签,并给数组赋值如下:
myArray = returnMyArray();
我想在c++中实现等效,即将存储在文件中的数组赋值给我的c++源代码中的数组,以便数据在编译时可用于执行。
我想在理论上我可以复制和粘贴(合适的格式)数组从文件到我的c++源代码,但他们太大了,这是不实际的。
我可以很容易地将文件重写为c++访问数据最容易的格式——要么用c++数组语法,要么每行一个字符串读入数组。
在类似的情况下,是否有一种简单的方法可以在终端中使用g++编译时包含包含自定义函数库的文件?(我的网络搜索显示了各种IDE应用程序的大量方法,但我在vim中编写源代码,并在命令行上使用g++编译)。
我很抱歉,如果这是微不足道的,我已经错过了它,但我被难住了!
谢谢。
我的结构如下:
文件: data.array
/* C++ style comments are ok in this file and will be ignored
* both single and multiline comments will work */
// the data in the array is a comma seperated list, lines can be any length
1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12,
// more comma seperated data
9996, 9997, 9998, 9999
文件: class.h
extern int myArray[]; // you should fill in the size if you can
// more stuff here
文件: class.cpp
// if you have an editor that highlights syntax and errors, it may not like this
// however, #include is handled before compiling and performs a blind substitution
// so this is perfectly legal and should compile.
// Visual C++ 2010 highlights this as an error, but the project builds fine.
int myArray[]
{
#include "data.array"
};
// other definitions of stuff in class.h