2014-10-18 2 views
0

나는 완전한 초보 프로그래머이므로 나와 함께 감당해야한다.배열의 입력 데이터 파일에서 값을 인쇄하는 방법은 무엇입니까?

그래서 program.exe < data.txt를 사용하여 명령 창에서 프로그램에 입력으로 사용할 입력 텍스트 파일이 있습니다. 텍스트 파일에는 5 개의 라인이 있고, 각 라인은 30.0 70.0 0.05와 같은 3 개의 double 값을 가지고 있습니다.

기본적으로 printf ("첫 번째 값은 % f" , array [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'가 아마 당신이 기대하는 것을 돌려주지 않을 것이라고 말할 수 있습니다. (여러분은 배열 요소의 메모리 주소를 출력 할 것입니다) – UnholySheep

+0

C 코드 작성을 돕기 위해. typedef 문을 사용하여 구조체를 정의하지 마십시오. 오히려 사용하십시오 : struct nameOfStruct {field of list}; 다음을 통해 구조체를 만듭니다. struct nameOfStruct myStructName; 참조 myStructName : struct myStrucName.fieldName ... 다음을 통해 myStructName에 대한 포인터를 생성합니다. struct nameOfStruct * myStructPtr; I.E. typedef 구조체의 사용 .... {...} myStructName; 감가 상각된다. – user3629249

+0

이 줄은 : soundsource_t soundsource [MAXSOURCES] [2]; 스택에 soundsource_t 구조체의 다차원 배열 인스턴스를 만듭니다. 그러나이 다차원 배열에는 실제 값이 없습니다. 그런 다음 코드에서이 배열의 내용을 인쇄하려고합니다. 이 배열에 내용이 없으므로 스택의 가비지/휴지통이 인쇄됩니다. – user3629249

답변

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 
관련 문제