문제

I want to store in or read information from a *.dat file that is in binary mode. The file has the following data (encoded in binary form):

License: 123456
Owner: John Doe
Value: 10000.00

License: IAMDOE
Owner: Jane Doe
Value: 20000.00

Here is my code:

#include <stdio.h>
#include <string.h>
#define MAXPLEN 80

typedef struct {    
    char none; /* used for registry initiated by '\0' ,should be ingnored */    
    char owner[MAXPLEN];
    char license[6];
    double value;
} veiculo_t;    


/* i need to use the following functions in the process */

void print_registry(veiculo_t *v)
{
    printf("Owner: %s - License: %.6s - Value: %.2lf \n", (*v).owner, (*v).license, (*v).value);
}

void read_registry(veiculo_t *v)
{
    char license2[8];
    printf("Name of the owner:\n");
    fgets((*v).owner,100,stdin);

    printf("License plate:\n");
    fgets(license2,10,stdin);
    memcpy(&(v->license),license2,6);

    printf("Value:\n");
    scanf("%lf",&(*v).value);
}

int op_menu() 
{
    int op;
    printf("\n0 - end\n");
    printf("1 - insert\n");
    printf("2 - print\n");
    printf("option: ");
    scanf("%d",&op);  
    getchar();        
    return op;
}

int main()
{
    int op;
    op_menu();

    if (op=1){
        /* !!!!!HELP HERE!!!!!! */
    }

    if (op=2){
        FILE *f=fopen("veic.dat", "rb");
        if (f == NULL) 
        {
            printf("Not opened!\n");
        }
        else
            /* !!!!!HELP HERE!!!!!! */
    }
}

dont know much about "streams"... A few hints would be apreciated!

도움이 되었습니까?

해결책

First of all, change the below otherwise you will never enter your "menu".

op = op_menu();    //op should be assigned by the return value of open_menu
if (op==1){...
if (op==2){...    // I believe you want '==' not '='

According the comments you have to invoke read_registry method. The added code:

    if (op==1){
        //insert
        veiculo_t *t = malloc(sizeof(veiculo_t));
        read_registry(t);
        FILE *f=fopen("veic.dat", "ab");
        if (f == NULL)
        {
            printf("open file failed\n");
        }
        fwrite(t, sizeof(char), sizeof(*t), f);
        free (t);
    }

    if (op==2){
        //print
        FILE *f=fopen("veic.dat", "rb");
        if (f == NULL)
        {
            printf("Not opened!\n");
        }
        else
        {
            veiculo_t *t = malloc(sizeof(veiculo_t));
            printf("sizeof t: %d\n", sizeof(*t));
            while(fread(t, sizeof(char), sizeof(*t), f))
                print_registry(t);
            free(t);
        }

The example code just for you refrence, I would like to provide a thinking more than an answer, but seems code always is clear and powerful. In below example I write file use append mode, if you want re-write file every time, change mode in fopen when write file.

Hope that helpful.

다른 팁

fwrite and fread can be used for your needs.

Here's an example program that illustrates their usage:

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

int main(int argc, char** argv)
{
   FILE* out = fopen("data.o", "wb");
   int i = atoi(argv[1]);
   fwrite(&i, sizeof(int), 1, out);
   fclose(out);

   FILE* in = fopen("data.o", "rb");
   fread(&i, sizeof(int), 1, in);
   fclose(in);

   printf("Number saved and restored from file: %d\n", i);
}

Results of executing the program:

>> ./test-51 2345
Number saved and restored from file: 2345
>> ./test-51 -200
Number saved and restored from file: -200

The size of the output file is, as you would expect, only 4 bytes.

>> wc data.o
0 1 4 data.o
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top