2016-04-01 6 views
0

Ich habe einen Auftrag aus einer Datei ähnlich eine Tabelle zu lesen:Lesespalten aus einer Datei in eine Struktur

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 
... 

, wo es 20 Kategorien von Werten (Masse, Durchmesser, Dichte ...) und 9 Planeten.

Ich versuche, die Daten in eine struct planet mit den 20 Komponenten (Masse, Durchmesser, Dichte ...) einzulesen und kompiliert, aber ich kann keine der Strukturkomponenten in der Ausgabe (wie p[2].A) so sieht es nicht so aus, als würde ich sogar die Daten aus der Datei einlesen (und ich habe sie definitiv an der richtigen Stelle gespeichert). Weil ich versuche, Spalten in den struct zu lesen, ist es ein bisschen chaotisch ...

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

...

Kann jemand ein Problem mit dieser Methode sehen?

+0

Fehlt den Daten wirklich das Tag 'planet' oder' name' aus der ersten Zeile? Ihr Layout Ihrer Struktur ist abscheulich; Leerzeichen ist billig und sollte verwendet werden. Die Namen der Strukturelemente sind opak. Es ist nicht klar, warum Element 'D' ein' float' ist, wenn alle anderen 'char' sind. Sie sollten ein 'NUM_PLANETS' anstelle von 9 haben (es ist interessant zu sehen, dass Pluto wieder zu einem Planeten wurde). Mir ist nicht klar, wie du Kommas überspringst. Hast du Drucken hinzugefügt? –

+0

Entschuldigung, die Kommas sind nicht wirklich in der Tabelle, ich habe es einfach so eingegeben, um Dinge zu trennen. Element D (Dichte) ist ein Gleitkommawert, da es das einzige Element ist, das als Wert verwendet wird, um die Strukturen zu sortieren (ich muss die Tabelle neu drucken, aber mit den Planeten, die nach absteigender Dichte geordnet sind). Der Rest der Elemente spielt keine Rolle, also lese ich sie als Strings, damit ich sie in der neuen Tabelle nachdrucken kann. Ich habe alles zu sortieren und fprinft in eine andere Datei geschrieben, und ich habe auch versucht, es einfach geradeaus zu drucken, aber nichts – ari

+0

und ja, es fehlt ein Etikett für die erste Zeile – ari

Antwort

0

Ihr unmittelbares Problem ist diese Schleife:

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

Sie j zu vergleichen, aber i erhöht wird. Verwenden Sie j++ und ändern Sie den Körper der Schleife. Oder verwenden Sie i überall.

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

Ihre for (i = 0; i < 20; i++) Schleife hat ein Problem zu:

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

Sie haben zwei Schritten i. Beachten Sie die Fehlerprüfung, die ich hinzugefügt habe.

Dies ist "Arbeits" -Code für den 6-Zeilen plus Planet Namen Datensatz, den Sie in der Frage haben.

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

} 

Die Probe Ausgang (von Echo der Eingänge):

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 

Es war einfach, die mal-gebildete Schleife steuert zu erkennen, wenn das Programm beansprucht alles ein Planet Name war.

Verwandte Themen