首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从数组中的输入数据文件中打印值?

如何从数组中的输入数据文件中打印值?
EN

Stack Overflow用户
提问于 2014-10-18 15:29:58
回答 2查看 46关注 0票数 0

我是一个完全的初学者程序员,所以请容忍我。

因此,我有一个输入文本文件,我将使用它作为程序的输入,在命令窗口中使用program.exe < data.txt。文本文件有5行,每一行有3个双值,比如30.0、70.0、0.05等等。

我想基本上使用一个结构数组来打印这些输入值,比如printf(“第一个值是%f",arrayi)。

以下是我目前不正确的代码:

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>

#define MAXSOURCES 100

typedef struct {
    double x;
    double y;
} coordinates_t;

typedef struct {
    coordinates_t point;
    double w;
} soundsource_t;

coordinates_t a;
soundsource_t b;

int main(int argc, char *argv[]) {
    int i;

    while(scanf("%lf %lf %lf", &a.x, &a.y, &b.w) == 3) {
        soundsource_t soundsource[MAXSOURCES][2];
        for (i = 0; i <= MAXSOURCES; i++) {
            printf("%d", soundsource[i][0]);
            printf("%d", soundsource[i][1]);
            printf("%d", soundsource[i][2]);
            printf("\n");
        }
    }
    return 0;
}

有人能帮我修好密码吗?谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-10-18 16:00:00

样品修复

代码语言:javascript
运行
复制
int main(int argc, char *argv[]) {
    soundsource_t soundsource[MAXSOURCES];
    int i, n = 0;

    while(scanf("%lf %lf %lf", &a.x, &a.y, &b.w) == 3) {
        soundsource[n].point = a;
        soundsource[n].w = b.w;
        n += 1;
    }
    for (i = 0; i < n; i++) {
        printf("%f,", soundsource[i].point.x);
        printf("%f ", soundsource[i].point.y);
        printf("%f\n",soundsource[i].w);
    }
    return 0;
}
票数 0
EN

Stack Overflow用户

发布于 2014-10-18 16:12:13

确保你读到了第一个评论。但是,您似乎还需要更多的研究,所以我将给您一个如何修改代码以实现这一目标的示例。

代码语言:javascript
运行
复制
#include <stdio.h>
#include <stdlib.h>

#define MAXSOURCES 100

typedef struct {
    double x;
    double y;
} coordinates_t;

typedef struct {
    coordinates_t point;
    double w;
} soundsource_t;

int main(int argc, char *argv[]) {
    int i, n = 0; // i is a counter and n will be the number of sources we actually read

    // we declare an array of sources, with size MAXSOURCES
    soundsource_t array[MAXSOURCES];

    /*
     * Now every cell of the array is a soundsource_t struct.
     * So, every cell is capable of storing a 'point' and a 'w'.
     */


    // We will read from stdin, three doubles per loop
    // and we will store them in the n-th cell of the array
    while(scanf("%lf %lf %lf", &(array[n].point.x), &(array[n].point.y), &(array[n].w)) == 3) {
        n++; // increment n, so that we keep track of number of sources we read
        if(n == MAXSOURCES) // we reached the limit, so stop reading
          break;
    }

    // print the sources we read. Notice that we go until n and not MAXSOURCES,
    // since we read n sources.
    for(i = 0; i < n; ++i) {
      // print the i-th cell
      printf("%lf %lf %lf\n", array[i].point.x, array[i].point.y, array[i].w);
    }

    return 0;
}

输出:

代码语言:javascript
运行
复制
0.1 0.2 0.3
0.7 0.8 0.9
e <-- here I typed a letter to stop reading from input
0.100000 0.200000 0.300000
0.700000 0.800000 0.900000
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26441387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档