2016-04-22 7 views
0

私はC言語で新しく、大学ではユーザーが導入したデータを保存してデータを読み込んで、うまく導入されているかどうかを確認できるプログラムを作成しています。その後、各空き領域にそのデータを保存する必要があります。データを保存するための空きスペースが10あります。保存されたすべてのデータを使用して、データも表示し、導入されたすべてのデータと比較する必要があります。私は導入されたデータを読むことの一部を作りました。私が持っている問題は、私がどのようにしてデータベースを作ってそのデータを表示できるか分からないということです。ここまでは私が今まで持っているコードです。Cでデータを保存して表示するプログラムを作成する

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

    typedef struct { 
     char name[30]; 
     int num_sample; 
     char section; 
     int cabin; 
     int day; 
     int month; 
     int year; 
    }Species; 

    int menu() { 

     int op = 0; 

     printf ("\nPrototype Nature Reserve.\n"); 
     printf ("1. Insert a new species.\n"); 
     printf ("2. List the species housed.\n"); 
     printf ("3. Show stadistics.\n"); 
     printf ("4. Exit.\n"); 
     printf ("\nIntroduce an option: "); 
     scanf ("%d", &op); 

     return op; 
    } 

    void New_species() { 

     Species e; 
     int i, j; 
     char species[100]; 
     char aux[10]; 
     printf("Enter data:\n"); 
     //A fflush is made to not have a problem with the number entered in the menu 
     fflush(stdin); 
     gets (species); 
     //While it is different from - read the text 
     for (i = 0; species[i] != '-'; i++) { 
      e.name[i] = species[i]; 
     } 
     e.name[i] = '\0'; 
     i++; 
     //We add 1 to the position i because is ending in - and should start reading from the following 
     for (j = 0; species[i] != '-'; i++,j++) { 
      aux[j] = species[i]; 
     } 
     aux[j] = '\0'; 
     e.num_sample = My_atoi(aux); 
     // Check that the sample is a number and not a character 
     if (e.num_sample <= 0 || e.num_sample >= 100) { 
      printf ("--> Error in data format.\n"); 
     } 
     i++; 
     //Reads the day introduced 
     for (j = 0; species[i] != '/'; i++, j++) { 
      aux[j] = species[i]; 
     } 
     aux[j] = '\0'; 
     e.day = My_atoi(aux); 
     //Controls the format of the day 
     if (e.day <= 0 || e.day > 31) { 
      printf ("--> Error in data format.\n"); 
     } 
     i++; 
     //Reads the month introduced 
     for (j = 0; species[i] != '/'; i++, j++) { 
      aux[j] = species[i]; 
     } 
     aux[j] = '\0'; 
     e.month = My_atoi(aux); 
     //Controls the format of the month 
     if (e.month <= 0 || e.month > 12) { 
      printf ("--> Error in data format.\n"); 
     } 
     i++; 
     //Reads the year introduced 
     for (j = 0; species[i] != '-'; i++, j++) { 
      aux[j] = species[i]; 
     } 
     aux[j] = '\0'; 
     e.year = My_atoi(aux); 
     //Controls the format of the year 
     if (e.year < 1970 || e.year > 2060) { 
      printf ("--> Error in data format.\n"); 
     } 
     i++; 
     //Reads the section introduced 
     e.section = species[i]; 
     //Controls that the section is in capital letters 
     if (e.section < 'A' || e.section > 'Z') { 
      printf ("--> Error in data format.\n"); 
     } 
     i+= 2; 
     //As the cabin is at the end it must reach the \0 
     for (j = 0; species[i] != '\0'; i++, j++) { 
      aux[j] = species[i]; 
     } 
     aux[j] = '\0'; 
     e.cabin = My_atoi(aux); 
     if (e.cabin < 0 || e.cabin > 20) { 
      printf ("--> Error in data format.\n"); 
     } 
     printf ("Species stored successfully (%d/10 free)."); 
     //This printf is just to ensure that the data entered was read correctly 
     printf ("\n%s", species); 
    } 

    int My_atoi(char cad[10]) { 
     int r = 0; 
     int i; 
     for (i = 0; cad[i] != '\0'; i++) { 
      r = r * 10; 
      r += cad[i] - '0'; 
     } 
     return r; 
    } 

    void list_species() { 

    } 

    void stadistics() { 

    } 

    void executeOption(int op) { 
     switch (op) { 
      case 1: 
       New_species(); 
       break; 
      case 2: 
       list_species(); 
       break; 
      case 3: 
       stadistics(); 
       break; 
      default: 
       break; 
     } 
    } 

    int main() { 
     int op = 0; 
     do { 
      op = menu(); 
      executeOption(op); 
     } while (op != 4); 
     return 0; 
    } 

私はそれを格納するための.txtファイルを作成することができますが、私はそれを使用する方法がわからないと、私はそれがこのプログラムで許可さだと思いませんので、あなたは*ファイルを使用できることを見てきました。

私はどのようにそれが必要work

感謝の写真を残しておきます。

+1

'にfflush(STDIN);' '無効New_speciesで()'この割り当ては、ファイルに関するものではありません場合は、 – sjsam

+1

を期待どおりに動作しない場合がありますが、それはおそらく10 'のグローバル配列を定義する問題ですデータを保存および検索するために使用します。 – Quentin

+1

また、 'Species e;'あなたは 'void New_species()'に対してローカルなオブジェクトを作成しています。関数スタックがクリアされると、オブジェクトはクリアされます。つまり、「e」は永続的ではありません。 – sjsam

答えて

0

ok okデータをファイルに保存してファイルから読み込む必要があります。ここ 私のショートコードとの読み取りおよび書き込み:

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


typedef struct worker 
{ 
int sal; 
char name[25]; 
}W; 


void main() 
{ 

FILE *f; 
int i,j=4; 
W a[3]; 

while(j!=3) 
{ 
printf("\nEnter\n[1]write\n[2]read\n[3]exit\n"); 
scanf("%d",&j);  
if(j==1) 
{ 
if (f==NULL) 
{ 
    printf("Error!!\n"); 
    exit(0); 
} 
f=fopen("workers.txt","w"); 
for(i=0;i<3;i++) 
{ 
printf("\nEnter worker name: "); 
scanf("%s",&a[i].name); 
printf("\nEnter worker sal: "); 
scanf("%d",&a[i].sal);  
fprintf(f,"%s %d",a[i].name,a[i].sal); 
}  

if(j==2) 
{ 
if (f==NULL) 
{ 
    printf("Error!!\n"); 
    exit(0); 
} 
f=fopen("workers.txt","r"); 
for(i=0;i<3;i++) 
{ 
fscanf(f,"%s %d",a[i].name,&a[i].sal); 
printf("\n%s %d",a[i].name,a[i].sal); 
} 
} 
} 

fclose(f);} 
0

と私はあなたが許可されませんなぜそれがこのプログラムに

を許さだと思いませんか?はい確かに、あなたは、これは、彼があなたに関数fprintfてテキストを書く方法を示して彼の答えにDORによって提案されたものですテキストファイルに二つの選択肢

を通過することができます。 >c-tutorial-binary-file-io

#include<stdio.h> 

/* Our structure */ 
struct rec 
{ 
    int x,y,z; 
}; 

int main() 
{ 
    int counter; 
    FILE *ptr_myfile; 
    struct rec my_record; 

    ptr_myfile=fopen("test.bin","wb"); 
    if (!ptr_myfile) 
    { 
     printf("Unable to open file!"); 
     return 1; 
    } 
    for (counter=1; counter <= 10; counter++) 
    { 
     my_record.x= counter; 
     fwrite(&my_record, sizeof(struct rec), 1, ptr_myfile); 
    } 
    fclose(ptr_myfile); 
    return 0; 
} 

からのデータをバック取得する - あなたがここでそれをチェックアウトすることができますあなたの周りにも、この例のようにfwriteの/ fseekをを使用してファイルに書き込むことができるバイナリファイルに

は、私がサーフィンを見つけましたチュートリアルでは、

#include<stdio.h> 

/* Our structure */ 
struct rec 
{ 
    int x,y,z; 
}; 

int main() 
{ 
    int counter; 
    FILE *ptr_myfile; 
    struct rec my_record; 

    ptr_myfile=fopen("test.bin","rb"); 
    if (!ptr_myfile) 
    { 
     printf("Unable to open file!"); 
     return 1; 
    } 
    for (counter=1; counter <= 10; counter++) 
    { 
     fread(&my_record,sizeof(struct rec),1,ptr_myfile); 
     printf("%d\n",my_record.x); 
    } 
    fclose(ptr_myfile); 
    return 0; 
} 
0

を示してあなたは私が提案を持っている.txtファイルで解決策を探しているなら、バイナリは、この道を行くことが

#include <stdio.h> 
int main(){ 
    FILE *in; 
    FILE *OUT = fopen("read_at.txt", "w"); 
    char name[20]; 
    char capture[80]; 
    int age = 0; 
    float grade = 0; 

    puts("introduce your name:"); 
    scanf("%19s", name); 
    puts("introduce your age:"); 
    scanf("%i", &age); 
    puts("introduce your Math grade(0-10):"); 
    scanf("%f", &grade); 

    fprintf(OUT, "Age %i, Grade %f, Name %s\r\n", age, grade, name); 
    fclose(OUT); 
    if(!(in = fopen("read_at.txt","r"))){ 
     fprintf(stderr, "The file could not be opened. \n"); 
     return 1; 
    } 
    while (fscanf(in, "Age %i, Grade %f, Name %20[^\n]\n", &age, &grade, name)==3){ 
     printf("A %i years old student has achieved a %f mark in last math exam, that student is: %s", age,grade, name); 
    } 
    fclose(in); 
} 

私が作成したこのコードでは、1つのファイルを作成し、具体的な形式のデータを読み込んで取得する方法を確認できます。データベースを探している場合は、値を ";"と改行(\ n)で区切って入力するだけです。それを.csvとして保存すると、Excelシートとかなり良いデータベースが得られます。 このソリューションに興味がある場合は、追加のヘルプが必要かどうかお知らせください。

よろしく、

関連する問題