【问题标题】:fscanf-peforming arithmetic operations before storing integers into an array在将整数存储到数组之前执行 fscanf 算术运算
【发布时间】:2018-02-03 00:56:24
【问题描述】:

嘿,我想知道是否有可能 函数 fscanf 对从文件中读取的整数进行操作,然后再将其存储到数组中。

例如:

for(int i = 0 ;i < size;i++){
    //Here it just stores the number read
    fscanf(file, "%d", &arr[i]);
    //However I'm trying to do such a thing(substract the number by 1 everytime)
    fscanf(file, "%d -1 ", &arr[i]);
    printf("\n%d",arr[i]);
}

提前致谢

【问题讨论】:

  • 呃.....没有。
  • 不,这是不可能的。读取输入,然后在您的代码中处理它。
  • 这是不可能的,但如果你愿意,你可以编写一个包装函数或宏来做到这一点:#define FSCANF_SUBTRACT_1(FILE, FMT, VAR_PTR) do { int result = fscanf(FILE, FMT, VAR_PTR); --*VAR_PTR; return result; } while (false)

标签: c arrays scanf


【解决方案1】:

这不能满足你的需要吗?

for(int i = 0; i < size; i++){
    int temp;
    fscanf(file, "%d", &temp);
    arr[i] = temp - 1;

    printf("\n%d",arr[i]);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 2012-06-19
    • 2020-08-16
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多