【问题标题】:Can't separate columns when reading txt file读取txt文件时无法分隔列
【发布时间】:2015-01-13 10:07:32
【问题描述】:

您好,我设法读取了 txt 文件并将它们打印在控制台窗口上。我的任务是选择和采样一定数量的数据。

txt文件示例:

Voltage (V),Current (I),Power (W)
50,2,100,
51,2,102,
52,2,104,

等..如何只显示电压和功率列?

【问题讨论】:

  • 示例:请出示代码。
  • 到目前为止你尝试过什么?它是如何工作的?怎么没用?对于示例输入文件,预期和实际输出是什么?
  • 探索strtok()strsep()

标签: c input printf scanf out


【解决方案1】:
#include <stdio.h>

int main(void) {
    int V, I, W;//float ?
    FILE *fp = fopen("data.txt", "r");

    while(fgetc(fp) != '\n')
        ;//skip first line

    while(3==fscanf(fp, "%d,%d,%d,", &V, &I, &W)){
        printf("V:%d, I:%d, W:%d\n", V, I, W);
    }
    fclose(fp);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-12
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多