2011-07-05 2 views
0

숫자가있는 텍스트 파일이 있습니다. 각 행에 두 개의 숫자가 공백으로 구분되어 있습니다. 숫자의 각 쌍은 (x, y) 좌표를 나타냅니다. 나는 내가 알고있는 언어이기 때문에, C이 쓰기 위해 노력하고 있어요,하지만 난 비주얼 스튜디오 있어요 2010 년 다음과 같이 내가 가진 코드는 다음과 같습니다텍스트 파일에서 double 형식으로 읽음 (C/Visual Studio 사용)

#include "stdafx.h" 
#include <stdio.h> 
#include <stdlib.h> 

#define MAXPOINTS 10 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    double points [MAXPOINTS]; 

    int i; 
    for (i = 0; i < MAXPOINTS; i++) { 
     points[i] = 0.0; 
    } 

    FILE* pFile; 
    pFile = fopen ("points.txt","r"); 

    if (pFile == NULL) 
    { 
     printf("Could not open file\n"); 
     return 0; 
    } 

    rewind (pFile); 

    i = 0; 
    while (fscanf(pFile, "%f %f", &points[i], &points[i + 1]) == 2) { 
     printf("blah\n"); 
     i = i + 2; 
    } 

    for (i = 0; i < MAXPOINTS; i++) { 
     printf("[%d] = %f\n", i, points[i]); 
    } 

    fclose (pFile); 
    return 0; 
} 

출력은 다음과 같습니다

blah 
blah 
blah 
[0] = 0.000000 
[1] = 0.000000 
[2] = 0.000000 
[3] = 0.000000 
[4] = 0.000000 
[5] = 0.000000 
[6] = 0.000000 
[7] = 0.000000 
[8] = 0.000000 
[9] = 0.000000 
숫자가 배열로 읽어되지 않는 이유

100 200 
300 400 
500 500 

내가 알아낼 수 없습니다 :

어디 points.txt 세 개의 행이 있습니다.

아이디어가 있으십니까?

답변

2

% f 형식은 float에 대한 포인터가 필요하며 double에 대한 포인터를 제공합니다.

관련 문제