-3

我正在开发一个数据库项目,我想在其中创建一个数组,如下所示:

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 文件中获得了这些数据。如果我要手动输入数据,可能需要数周时间;有没有办法通过某种方式为每个元素插入逗号和引号?

4

1 回答 1

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。

于 2012-07-28T05:17:47.850 回答