fscanf无法读取选项卡值

时间:2016-11-29 14:48:07

标签: c file io scanf

我正在编写一个简单的C程序,使用fscanf从选项卡文件中读取浮点值。

以下是标签文件:

>     7             <--------------this declares the number of pairs of values
>     4.0     5.0
>     5.5     4.0
>     1.0     5.0
>     5.0     2.5
>     2.0     2.0
>     4.0     2.0
>     1.0     1.5

这是我的计划:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <string.h>

char filename[100];
FILE *fp;
int n=0;
int N;
double x[22];
double y[22];

int main(void) {
    printf("Input filename: ");
    fgets(filename, 100, stdin); //read filename
                                 //check if enter sign exist in filename

    N = (int)strlen(filename);
    for (int i = 0; i < N; i++) {
        if (filename[i] == '\n') {
            filename[i] = '\0';
        }
    }

    //printf("%s", filename);
    fp = fopen(filename, "r"); //open file
    if (fp == NULL) //check file
    {
        printf("\nCannot open input file %s\n", filename);
        exit(1);
    }

    fscanf(fp, "%d", &n);  //read Data
    for (int i = 0; i < n; i++) {
        fscanf(fp,"%f%f", &x[i], &y[i]);
    }

    fclose(fp); //close file
                //check data retrieved
    for (int i = 0;i < n;i++) {
        printf("%f//%f//\n", x[i], y[i]);
    }

结果是14个值0.0000而不是选项卡文件中的值。

0 个答案:

没有答案