Thread: Copying an int on a fich

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    4

    Copying an int on a fich

    How I can write (or read) on a fich an int??

    I want to write (or read) in to the fich "int numero"

    Code:
    struct contfich{
        char nombre[100];
        char apellidos [100];
        int numero;
        char telefono[10];
      };
    
     
    struct contlist{
        char nombre[100];
        char apellidos [100];
        int numero;
        char telefono[10];
        struct contlist *sig;
      };
    Code:
    void insertarlista(struct contlist**plista, struct contfich c){
    struct contlist *nuevo;
       nuevo=(struct contlist*)malloc(sizeof(struct contlist));
       strcpy((*nuevo).nombre, c.nombre);
       strcpy((*nuevo).apellidos, c.apellidos);
       strcpy((*nuevo).telefono, c.telefono);
       nuevo->sig=*plista;
       *plista=nuevo;
    }
    
    
    void guardarlista (struct contlist * lista){ 
      FILE*fich;
      char nombrefich[100];
      struct contlist *aux;
      struct contfich c;
       printf("Introduce nombre de la lista:  ");
       scanf("%s", nombrefich);
       fich=fopen(nombrefich, "wb");
       aux=lista;
         while(aux!=NULL){
            strcpy(c.nombre, aux->nombre);
            strcpy(c.apellidos, aux->apellidos);
            strcpy(c.telefono, aux->telefono);
    	    fwrite(&c,sizeof(struct contfich),1,fich);
    	    aux=aux->sig;
        } 
      fclose(fich);
      printf("\nLista '%s' guardada!!\n\n", nombrefich);
    system("PAUSE");
    }

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by carlosmarin7 View Post
    How I can write (or read) on a fich an int??

    I want to write (or read) in to the fich "int numero"

    Code:
    struct contfich{
        char nombre[100];
        char apellidos [100];
        int numero;
        char telefono[10];
      };
    
     
    struct contlist{
        char nombre[100];
        char apellidos [100];
        int numero;
        char telefono[10];
        struct contlist *sig;
      };
    Code:
    void insertarlista(struct contlist**plista, struct contfich c){
    struct contlist *nuevo;
       nuevo=(struct contlist*)malloc(sizeof(struct contlist));
       strcpy((*nuevo).nombre, c.nombre);
       strcpy((*nuevo).apellidos, c.apellidos);
       strcpy((*nuevo).telefono, c.telefono);
       nuevo->sig=*plista;
       *plista=nuevo;
    }
    
    
    void guardarlista (struct contlist * lista){ 
      FILE*fich;
      char nombrefich[100];
      struct contlist *aux;
      struct contfich c;
       printf("Introduce nombre de la lista:  ");
       scanf("%s", nombrefich);
       fich=fopen(nombrefich, "wb");
       aux=lista;
         while(aux!=NULL){
            strcpy(c.nombre, aux->nombre);
            strcpy(c.apellidos, aux->apellidos);
            strcpy(c.telefono, aux->telefono);
            fwrite(&c,sizeof(struct contfich),1,fich);
            aux=aux->sig;
        } 
      fclose(fich);
      printf("\nLista '%s' guardada!!\n\n", nombrefich);
    system("PAUSE");
    }
    By "fich" I assume you mean "file"? If so, you can simply read/write the entire structure to file using "fwrite". Also, you don't need to make a temporary copy of the structure before writing it to the file; you can just "fwrite" "aux" directly. One more thing, instead of manually copying each field of the struct, just use "memcpy" to get it all in one go...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM

Tags for this Thread