簡體   English   中英

將文件中的列讀入結構

[英]Reading columns from a file into a struct

我有一項任務要從文件中讀取類似於以下內容的表:

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

...

誰能看到這種方法的問題?

您的直接問題是此循環:

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

當程序聲稱一切都是行星名稱時,很容易發現格式錯誤的循環控件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM