如何从文件中正确读取双精度数组?

时间:2019-06-04 04:42:04

标签: c arrays printf double

我正在尝试从文件中读取一个数组,然后将这些值分配给另一个数组。 我得到很多零和非常大的数字。

我尝试了不同的说明符,但它们仍然相同。 当没有分配并且只打印一个数组时,就没有这样的问题

$filter      = ["min_price" => "1.98"];
$editions    = [1,2,10];

$editions = array_combine(
    array_map(function($i){ return ':id'.$i; }, array_keys($editions)),
    $editions
);
$in_placeholders = implode(',', array_keys($editions));
$sql = "SELECT * FROM books WHERE price >= :min_price AND edition IN ($in_placeholders)";
$stm = $pdo->prepare($sql);
$stm->execute(array_merge($filter,$editions));
$data = $stm->fetchAll();

看起来像是在分配值,但无法打印第一个数组 enter image description here

1 个答案:

答案 0 :(得分:2)

无法直接读取复数

// read a double (%lf) into a complex is wrong
fscanf(fo, "%lf", &mas[N]);

您需要分别阅读每个部分,(假设文件内容的格式为“ 3.14159-2.71828i”)

// read a complex parts
double r, c;
if (fscanf(fo, "%lf%lfi", &r, &c) != 2) /* error */;
// join the parts
mas[N] = r + c*I;