2014-10-18 55 views
0

我是一个完整的初学者程序员,所以忍受着我。如何从数组中输入数据文件打印值?

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

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

这里是我的不正确的代码至今:

#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; 
} 

有人可以帮我解决我的代码?由于

+0

为了帮助你,你首先要告诉我们什么是错的代码。你期待什么输出,运行时会发生什么,等等。蝙蝠的权利我可以告诉那些'printf's可能不会返回你期望的结果(它们会打印你的数组元素的内存地址) – UnholySheep 2014-10-18 15:33:02

+0

只是为了帮助你用你的C编码。不要使用typedef语句来定义结构。而是使用:struct nameOfStruct {字段列表};然后通过以下方式创建结构:struct nameOfStruct myStructName;通过以下方式引用myStructName:struct myStrucName.fieldName ...然后通过以下命令创建一个指向myStructName的指针:struct nameOfStruct * myStructPtr; I.E.使用typedef结构.... {...} myStructName;折旧。 – user3629249 2014-10-18 15:59:43

+0

this line:soundsource_t soundsource [MAXSOURCES] [2];在堆栈上创建一个多维数组soundsource_t结构的实例。但是,这个多维数组中没有实际值。您的代码然后尝试打印此数组的内容。由于该数组中没有放置任何内容,所以可能发生的最好情况是将打印堆栈中的垃圾/垃圾。 – user3629249 2014-10-18 16:06:39

回答

0

样品固定

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

请务必阅读第一条评论。但是,您似乎需要学习更多,所以我会举例说明如何修改代码以实现此目的。

#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; 
} 

输出:

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