2016-05-03 89 views
-1

我試圖將.txt文件存儲到struct中,但未成功。 我不需要存儲第一行,但我確實需要其餘的。 謝謝你們! =)將.txt文件存儲到結構中

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

int main(int argc, const char * argv[]) { 
    // insert code here... 
    printf("Hello, World!\n"); 

    struct candidates 
    { 
     char name[25]; 
     char gender[5]; 
     int height; 
     int weight; 
    }; 

    struct candidates values[25]; 

    FILE *fp; 
    if ((fp=fopen("candidatesdata.txt","r"))==NULL){printf("Unable to open file\n");} 


    int x = 0; 
    int iterations = 0; 
    while((fscanf(fp,"%s,%s,%d,%d",&values[x].name, &values[x].gender, &values[x].height, &values[x].weight))!=EOF) 
     { 
      x++; 
      iterations +=1; 
     } 
    fclose(fp); 

    //values[15].weight = 300; 

    printf("\n%d\t%d",iterations,values[1].height); 


    return 0; 
} 

文本文件如下所示:

Name,Gender,Height,Weight 
Tanner,M,71.8,180.25 
John,M,70.75,185.3 
Parker,F,65.25,120.3 
Meeks,M,57.25,210.2 
+2

而你的闕stion /錯誤是? – ckruczek

+5

你的'fscanf'預計身高和體重的整數,但他們顯然是floatin數字。難怪 – Aif

+0

,但結構仍然不存儲在txt正確的數據 –

回答

0
struct candidates 
    { 
     char name[25]; 
     char gender[5]; // can be char gender too !! 
     int height; // should be changed to float height 
     int weight; // should be changed to float height 
    }; 

如果實際上有:

Name,Gender,Height,Weight 

在你的文件,你可能會做他你去while循環之前:

char somebigstring[100]; 
. 
. 
. 
if(fscanf(fp,"%s",somebigstring) == EOF) //wasting the first line 
    exit(-1); // exiting here if there is no first line 
+0

哦,我看到了,我想我可以做一個do/while循環 –

0

此代碼做了兩件以下的事情;首先是將數據寫入.txt文件,第二步是從.txt文件讀取數據,並以結構數據類型存儲,這是您嘗試實現的結果,但僅適用於單個實體。接下來,您應該像實體一樣執行此操作。

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

typedef struct{ 
    char name[30]; 
    //other fields 
}Candidate; 

typedef struct{ 
    Candidate c; 
    char city[30]; 
    int vote; 
}CityVotes; 

//for .bin operations 
void write_to_file(FILE *f) 
{ 
    Candidate c; 
    CityVotes cv; 

    printf("Enter candidate name : "); 
    scanf("%s",&c.name); 
    cv.c = c; 
    printf("Enter the city : "); 
    scanf("%s",&cv.city); 
    printf("Enter the vote : "); 
    scanf("%d",&cv.vote); 
    fwrite(&cv, sizeof(cv), 1, f); 
} 
//for .txt operations 
void write_to_file1(FILE *f) 
{ 
    Candidate c; 
    CityVotes cv; 

    printf("Enter candidate name : "); 
    scanf("%s",c.name); 
    cv.c = c; 
    printf("Enter the city : "); 
    scanf("%s",&cv.city); 
    printf("Enter the vote : "); 
    scanf("%d",&cv.vote); 
    fprintf(f,"%s ", cv.c.name); 
    fprintf(f,"%s ", cv.city); 
    fprintf(f,"%d ", cv.vote); 
} 
//for .bin operations 
void read_file(FILE *f) 
{ 
    CityVotes cv; 
    while(fread(&cv, sizeof(cv), 1, f)){ 
     printf("Candidate : %s\t",cv.c.name); 
     printf("City.: %s\t",cv.city); 
     printf("Vote.: %d\n",cv.vote); 
    } 
} 
//for .txt operations 
void read_file1(FILE *f){ 
    CityVotes cv; 
    fscanf(f," %s", &cv.c.name); 
    fscanf(f," %s", &cv.city); 
    fscanf(f," %d", &cv.vote); 

    printf("Candidate : %s\t",cv.c.name); 
    printf("City.: %s\t",cv.city); 
    printf("Vote.: %d\n",cv.vote); 
} 
int main(void) 
{ 
    FILE *f; 
    f=fopen("candidates.txt","w"); 
    write_to_file1(f); 
    fclose(f); 
    f=fopen("candidates.txt","r"); 
    read_file1(f); 
    return 0; 
} 

請注意,代碼只是示例,您應該檢查是否爲空值。

+0

沒有解釋代碼可能導致進一步的追求離子,例如,爲什麼當'CityVotes'結構可能只有另一個'char []'時有'Candidate'結構。這個答案更適合作爲評論與您回答的問題的鏈接。 –

+0

這是正確的,你可以擺脫該結構,並在CityVotes中放置一個char [],但他或她可能想要添加更多的字段,它將在Candidate結構中有意義,但不在CityVotes中。例如 - >性別,身高,年齡等 –

0

修復這樣的:

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

struct candidates{ 
    char name[25]; 
    char gender[7];//Female+1 
    float height; 
    float weight; 
}; 

int main(void) { 
    struct candidates values[25]; 

    FILE *fp; 
    if ((fp=fopen("candidatesdata.txt","r"))==NULL){ 
     fprintf(stderr, "Unable to open file\n"); 
     exit(EXIT_FAILURE); 
    } 

    char line[64]; 
    int x = 0; 
    int iterations = 0; 
    while(fgets(line, sizeof line, fp)){ 
     iterations += 1; 
     if(x < 25 && sscanf(line, "%24[^,],%6[^,],%f,%f", values[x].name, values[x].gender, &values[x].height, &values[x].weight)==4){ 
      x++; 
     } else if(iterations != 1){ 
      fprintf(stderr, "invalid data in %d line: %s", iterations, line); 
     } 
    } 
    fclose(fp); 

    for(int i = 0; i < x; ++i){ 
     printf("%s,%c,%.2f,%.2f\n", values[i].name, *values[i].gender, values[i].height, values[i].weight); 
    } 

    return 0; 
}