繁体   English   中英

从文本文件中读取双精度(使用 C / Visual Studio)

[英]read in doubles from a text file (using C / Visual Studio)

我有一个带有数字的文本文件:每行有两个数字,用空格分隔。 每对数字代表一个 (x, y) 坐标。 我试图在 C 中写这个,因为它是我知道的语言,但我在 Visual Studio 2010 中工作。我拥有的代码如下:

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

output 是:

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

其中 points.txt 有三行:

100 200
300 400
500 500

我无法弄清楚为什么没有将数字读入数组。

有任何想法吗?

%f 格式需要指向浮点数的指针,而您将指针指向双精度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM