从文件中读取列到结构中

时间:2016-04-01 03:00:15

标签: c scanf

我有一个从文件中读取类似于:

的表的赋值
    MERCURY VENUS   EARTH   MARS    JUPITER SATURN  URANUS  NEPTUNE PLUTO
Mass(10^24kg)   0.33    4.87    5.97    0.642   1898    568 86.8    102 0.0146
Diameter(km)    4879    12104   12756   6792    142984  120536  51118   49528   2370
Density(kg/m^3) 5427    5243    5514    3933    1326    687 1271    1638    2095
Gravity(m/s^2)  3.7 8.9 9.8 3.7 23.1    9   8.7 11  0.7
Escape_Velocity(km/s)   4.3 10.4    11.2    5   59.5    35.5    21.3    23.5    1.3
Rotation_Period(hours)  1407.6  -5832.5 23.9    24.6    9.9 10.7    -17.2   16.1    -153.3
...

其中有20类值(质量,直径,密度......) 和9个行星。

我正在尝试将数据读入带有20个组件(质量,直径,密度......)的struct planet并编译但我无法在输出中打印任何结构组件(比如p[2].A)所以看起来我甚至没有读取文件中的数据(我肯定将它保存在正确的位置)。因为我正在尝试将列读入struct,所以它有点混乱......

typedef struct {char A[30]; char B[10]; char C[10]; float D; char E[10]; char F[10]; char G[10]; char H[10]; char I[10]; char J[10]; char K[10]; char L[10]; char M[10]; char N[10]; char O[10]; char P[10]; char Q[10]; char R[10]; char S[10]; char T[4]; char U[4];}planet;

int main(void) {
FILE * fp;
FILE * fs;
int i=0, j=0;
planet p[9];
char label[20][30];
char x[20][30];

fp=fopen("planets.txt", "r");
if (fp==NULL)printf("ERROR\n");

for(j=0; j<9; i++){                             //this for loop read the planet names
    fscanf(fp, "%s", p[i].A);
    }

for(i=0; i<20; i++){                            //this for loop counts the rows and reads the labels
    fscanf(fp, "%s", label[i]);                 //label[0] corresponds to .B values
    for(j=0; j<9; j++){                         //this for loop reads values across the rows and assigns them to the labels
        fscanf(fp, "%s", x[j]);
        if (i==0)strcpy(p[j].B, x[j]);
        else if (i==1) strcpy(p[j].C, x[j]);
        else if (i==2){
            p[j].D=atof(x[j]);
        }
        else if (i==3) strcpy(p[j].E, x[j]);
        else if (i==4) strcpy(p[j].F, x[j]);
        else if (i==5) strcpy(p[j].G, x[j]);
        else if (i==6) strcpy(p[j].H, x[j]);
        else if (i==7) strcpy(p[j].I, x[j]);
        else if (i==8) strcpy(p[j].J, x[j]);
        else if (i==9) strcpy(p[j].K, x[j]);
        else if (i==10) strcpy(p[j].L, x[j]);
        else if (i==11) strcpy(p[j].M, x[j]);
        else if (i==12) strcpy(p[j].N, x[j]);
        else if (i==13) strcpy(p[j].O, x[j]);
        else if (i==14) strcpy(p[j].P, x[j]);
        else if (i==15) strcpy(p[j].Q, x[j]);
        else if (i==16) strcpy(p[j].R, x[j]);
        else if (i==17) strcpy(p[j].S, x[j]);
        else if (i==18) strcpy(p[j].T, x[j]);
        else if (i==19) strcpy(p[j].U, x[j]);
    }
    i++;
}

...

有人能看到这个方法有问题吗?

1 个答案:

答案 0 :(得分:0)

你的直接问题是这个循环:

for (j = 0; j < 9; i++)

您正在比较j,但正在增加i。使用j++并更改循环体。或者始终使用i

    for (j = 0; j < 9; j++)
    {
        if (fscanf(fp, "%s", p[j].A) != 1)
        {
            fprintf(stderr, "Failed to scan a planet name\n");
            return 1;
        }
        printf("Planet name: [%s]\n", p[j].A);
    }

您的for (i = 0; i < 20; i++)循环也存在问题:

    for (i = 0; i < 20; i++)
    {
        printf("Line %d\n", i+2);
        if (fscanf(fp, "%s", label[i]) != 1)
        {
            fprintf(stderr, "Failed to read a label\n");
            return 1;
        }
        for (j = 0; j < 9; j++)                 // this for loop reads values across the rows and assigns them to the labels
        {
            if (fscanf(fp, "%s", x[j]) != 1)
            {
                fprintf(stderr, "Failed to read value for %s\n", p[j].A);
                return 1;
            }
            printf("Value: [%s] for %s\n", x[j], p[j].A);
            if (i == 0)
                strcpy(p[j].B, x[j]);
            …
        }
        i++;
    }

i有两个增量。请注意我添加的错误检查。

这是您在问题中使用的6行加行星名称数据的“工作”代码。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
    char A[30];
    char B[10];
    char C[10];
    float D;
    char E[10];
    char F[10];
    char G[10];
    char H[10];
    char I[10];
    char J[10];
    char K[10];
    char L[10];
    char M[10];
    char N[10];
    char O[10];
    char P[10];
    char Q[10];
    char R[10];
    char S[10];
    char T[4];
    char U[4];
} planet;

int main(void)
{
    FILE *fp;
    int i = 0, j = 0;
    planet p[9];
    char label[20][30];
    char x[20][30];
    const char filename[] = "planets.txt";

    fp = fopen(filename, "r");
    if (fp == NULL)
    {
        fprintf(stderr, "Failed to open file %s\n", filename);
        return 1;
    }

    for (j = 0; j < 9; j++)                     // this for loop read the planet names
    {
        if (fscanf(fp, "%s", p[j].A) != 1)
        {
            fprintf(stderr, "Failed to scan a planet name\n");
            return 1;
        }
        printf("Planet name: [%s]\n", p[j].A);
    }

    for (i = 0; i < 6; i++)                    // this for loop counts the rows and reads the labels
    {
        printf("Line %d\n", i+2);
        if (fscanf(fp, "%s", label[i]) != 1)
        {
            fprintf(stderr, "Failed to read a label\n");
            return 1;
        }
        printf("Label: [%s]\n", label[i]);
        for (j = 0; j < 9; j++)                 // this for loop reads values across the rows and assigns them to the labels
        {
            if (fscanf(fp, "%s", x[j]) != 1)
            {
                fprintf(stderr, "Failed to read value for %s\n", p[j].A);
                return 1;
            }
            printf("Value: [%s] for %s\n", x[j], p[j].A);
            if (i == 0)
                strcpy(p[j].B, x[j]);
            else if (i == 1)
                strcpy(p[j].C, x[j]);
            else if (i == 2)
                p[j].D = atof(x[j]);
            else if (i == 3)
                strcpy(p[j].E, x[j]);
            else if (i == 4)
                strcpy(p[j].F, x[j]);
            else if (i == 5)
                strcpy(p[j].G, x[j]);
            else if (i == 6)
                strcpy(p[j].H, x[j]);
            else if (i == 7)
                strcpy(p[j].I, x[j]);
            else if (i == 8)
                strcpy(p[j].J, x[j]);
            else if (i == 9)
                strcpy(p[j].K, x[j]);
            else if (i == 10)
                strcpy(p[j].L, x[j]);
            else if (i == 11)
                strcpy(p[j].M, x[j]);
            else if (i == 12)
                strcpy(p[j].N, x[j]);
            else if (i == 13)
                strcpy(p[j].O, x[j]);
            else if (i == 14)
                strcpy(p[j].P, x[j]);
            else if (i == 15)
                strcpy(p[j].Q, x[j]);
            else if (i == 16)
                strcpy(p[j].R, x[j]);
            else if (i == 17)
                strcpy(p[j].S, x[j]);
            else if (i == 18)
                strcpy(p[j].T, x[j]);
            else if (i == 19)
                strcpy(p[j].U, x[j]);
        }
    }

}

示例输出(来自回显输入):

Planet name: [MERCURY]
Planet name: [VENUS]
Planet name: [EARTH]
Planet name: [MARS]
Planet name: [JUPITER]
Planet name: [SATURN]
Planet name: [URANUS]
Planet name: [NEPTUNE]
Planet name: [PLUTO]
Line 2
Label: [Mass(10^24kg)]
Value: [0.33] for MERCURY
Value: [4.87] for VENUS
Value: [5.97] for EARTH
Value: [0.642] for MARS
Value: [1898] for JUPITER
Value: [568] for SATURN
Value: [86.8] for URANUS
Value: [102] for NEPTUNE
Value: [0.0146] for PLUTO
Line 3
Label: [Diameter(km)]
Value: [4879] for MERCURY
Value: [12104] for VENUS
Value: [12756] for EARTH
Value: [6792] for MARS
Value: [142984] for JUPITER
Value: [120536] for SATURN
Value: [51118] for URANUS
Value: [49528] for NEPTUNE
Value: [2370] for PLUTO
Line 4
Label: [Density(kg/m^3)]
Value: [5427] for MERCURY
Value: [5243] for VENUS
Value: [5514] for EARTH
Value: [3933] for MARS
Value: [1326] for JUPITER
Value: [687] for SATURN
Value: [1271] for URANUS
Value: [1638] for NEPTUNE
Value: [2095] for PLUTO
Line 5
Label: [Gravity(m/s^2)]
Value: [3.7] for MERCURY
Value: [8.9] for VENUS
Value: [9.8] for EARTH
Value: [3.7] for MARS
Value: [23.1] for JUPITER
Value: [9] for SATURN
Value: [8.7] for URANUS
Value: [11] for NEPTUNE
Value: [0.7] for PLUTO
Line 6
Label: [Escape_Velocity(km/s)]
Value: [4.3] for MERCURY
Value: [10.4] for VENUS
Value: [11.2] for EARTH
Value: [5] for MARS
Value: [59.5] for JUPITER
Value: [35.5] for SATURN
Value: [21.3] for URANUS
Value: [23.5] for NEPTUNE
Value: [1.3] for PLUTO
Line 7
Label: [Rotation_Period(hours)]
Value: [1407.6] for MERCURY
Value: [-5832.5] for VENUS
Value: [23.9] for EARTH
Value: [24.6] for MARS
Value: [9.9] for JUPITER
Value: [10.7] for SATURN
Value: [-17.2] for URANUS
Value: [16.1] for NEPTUNE
Value: [-153.3] for PLUTO

当程序声称一切都是行星名称时,很容易发现形成错误的循环控件。