2016-04-01 46 views
0

我有一個分配從文件讀取類似表:讀從文件列插入結構

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顆行星。

我試圖將數據讀入struct planet與20個組件(質量,直徑,密度等),它編譯,但我不能打印輸出中的任何結構組件(如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++; 
} 

...

任何人都可以看到這種方法有問題?

+0

數據是否真的缺少第一行的標記'planet'或'name'?你的結構佈局是可惡的;白色空間很便宜,應該使用。結構元素的名稱是不透明的。目前尚不清楚爲什麼元素D是'float'時,其他所有元素都是'char'。你應該有一個'NUM_PLANETS'而不是9個(有趣的是冥王星已經恢復爲行星)。我不清楚你是如何跳過逗號的。你有沒有添加打印? –

+0

對不起,這些逗號實際上並不在表格中,我只是將它鍵入來分隔事物。元素D(密度)是一個浮點數,因爲它是唯一被用作排序結構值的元素(我需要重新打印表格但行星按降密度排序)。其餘的元素無關緊要,所以我將它們作爲字符串讀取,以便我可以在新表格中重新打印它們。 我寫了一切排序和fprinft到另一個文件,我也試圖只是printf它直接退出,但沒有得到任何 – ari

+0

是的,它缺少第一行的標籤 – ari

回答

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 

當程序權利一切是一個行星名稱很容易被發現的惡意形成環路的控制。