在C中创建一个保存和显示数据的程序

时间:2016-04-22 13:56:15

标签: c

我是C的新人,在大学里,他们让我们创建了一个程序,可以保存用户引入的数据,读取数据并检查是否已经很好地介绍了数据。之后,它必须在每个空闲空间中保存该数据,您有10个空白空间用于保存数据。保存所有数据后,它还必须显示数据并将其与引入的所有数据进行比较。我做了部分阅读介绍的数据和cheking如果它的确定。我遇到的问题是我不知道如何建立数据库并显示数据。这是我到现在为止的代码。

    #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

感谢。

3 个答案:

答案 0 :(得分:0)

确定你需要将数据保存到文件并从文件加载。 这里是我的简短代码和读写:

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

答案 1 :(得分:0)

  

我并不认为这个程序允许这样做

为什么你不被允许?是的,你确实可以通过两种选择

发送文字 这是dor在他的回答中提出的,他向您展示了通过fprintf编写文本的方式。

到二进制文件 您也可以使用fwrite / fseek写一个文件,就像这个例子我发现你周围的冲浪可以在这里查看 - &gt; 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;
}

并从二进制文件中检索数据可能会像教程显示那样进行

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

答案 2 :(得分: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);
}

在我刚刚创建的这段代码中,您可以看到如何创建一个文件并在具有特定格式的情况下读取并获取其数据。如果要查找数据库,可以将值分隔为“;”和新行(\ n)。如果将它保存为.csv,您将获得一个excel表和相当不错的数据库。 如果您对此解决方案感兴趣,请告诉我您是否需要任何其他帮助。

此致