how can I save struct in file … C lang

前端 未结 6 849
悲&欢浪女
悲&欢浪女 2021-01-02 18:23

I\'d like to save a struct in a file. I\'d like to realize a function which makes this work. I tried this code but it didn\'t work.

struct utilisateur   //         


        
6条回答
  •  抹茶落季
    2021-01-02 18:38

    Try this, and then if that's not doing what you want, try explaining how it differs from what you want.

    void crea_fich(struct utilisateur *Tutilis)
    {
       FILE *f;
       size_t nwritten;
    
       f = fopen("futilis.dat","wb");
    
       if (f == NULL)
       {
          fprintf(stderr, "Cannot open file for writing.\n");
          exit(1);
       }
    
       nwritten = fwrite(Tutilis, sizeof Tutilis[0], 1, f);
       fclose(f);
    
       if (nwritten < 1)
       {
          fprintf(stderr, "Writing to file failed.\n");
          exit(1);
       }
    }
    

提交回复
热议问题