我正在开发一个数据库项目,我想按如下方式创建一个数组:
unsigned char* drillSize[][10] = {
{"0.3678", "23.222", "MN", "89000", ".000236", "678", "ZX", "8563", "LX", "0.678"},
{"0.3678", "23.222", "MN", "89000", ".000236", "678", "ZX", "8563", "LX", "0.678"},
.
.
.
//around 6000 rows }
我在Microsoft Word文件中提供了此数据。如果我手动输入数据,可能需要数周时间;有没有办法通过某种方式为每个元素插入逗号和引号?
答案 0 :(得分:0)
您可以使用正则表达式进行尝试。
如果您的数据以任何方式构建,例如csv文件,您可以通过某种方式将其导入程序,然后使用基本字符串函数对其进行切片。
在php中我
$input = file_get_contents($myfile) //lets say this contains a text: 1,2,3,4,5
$sliced = explode(",",$input);
$myarray = null; //our variable
$output = "\$myarray = new array("; //creating a template for an array as a String
//some logic with the $sliced array if you need
...
$output .= implode(",",$sliced); //we put it back as string
$output .= ");"; //close the array
eval($output); //after this its in php's scope
print_r($myarray);
基本上这就是你需要的更复杂的形式。如果文本不是那种结构,你可能需要一些C的正则表达式库,但是我建议用Python,Perl或者有更多支持和更灵活的东西创建一个文本,然后手动将代码复制回C。