Fichero guarda basura en vez de int (en C)

Iniciado por locodeltacho, 14 Septiembre 2015, 00:53 AM

0 Miembros y 1 Visitante están viendo este tema.

locodeltacho

Buenas gente! llevo un rato buscando una solución a un problema que me surgió haciendo la tarea de programación.
Me mandaron a hacer varias funciones con archivos. Una que cree un archivo, otra que lo abra, otra para agregar registros y una que los muestre.

Estoy bastaaaante perdido en el tema, pero logré que se cree el archivo y guarde algunos datos. El problema es que lo que es char lo guarda bien, pero lo que es int lo guarda como basura. (además de otros, como que no se hacer la función de "abrir", asi que decidi hacer un fopen en cada funcion xD)
Cuando estoy usando el programa


Despues de cerrar y leer de nuevo el archivo...



acá el codigo:
Main.c

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

int main()
{
    menu();
}


Libreria

#ifndef STRUCT_H_INCLUDED
#define STRUCT_H_INCLUDED

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

typedef struct{
                char nombre[45];
                char apellido[45];
                int edad;
                int nreg;
}t_registro;

void menu();
int CrearArchivo();
int AbrirArchivo(t_registro *reg);
int AgregarRegistro(t_registro *reg);
int MostrarRegistro(t_registro const *reg);

#endif // STRUCT_H_INCLUDED


Funciones

#include "struct.h"

void menu()
{
    char sele;

    t_registro registro;

    do
    {
        puts("\t \n Menu de Opciones");
        puts("\t \n a) Crear archivo");
        puts("\t \n b) Abrir archivo");
        puts("\t \n c) Agregar registro");
        puts("\t \n d) Mostrar registro");
        puts("\t \n e) Buscar registro por apellido");
        puts("\t \n 0) Salir");
        fflush(stdin);
        scanf("%c",&sele);
        if((sele>='a'&&sele<='e')||sele=='0')
        {


            switch(sele)
            {
            case 'a':
                CrearArchivo(&registro);
                break;
            case 'b':
                AbrirArchivo(&registro);
                break;
            case 'c':
                AgregarRegistro(&registro);
                break;
            case 'd':
                MostrarRegistro(&registro);
                break;
            case 'e':
                BuscarApellido(&registro);
                break;

            }
            fflush(stdin);
        }
        else
        {
            printf("\n Opcion incorrecta. ... \n \n");
            fflush(stdin);
        }
    }
    while(sele!='0');
}
////////////////

int CrearArchivo(t_registro *reg)
{
    FILE *pf;

    pf=fopen("archivo.dat","wb");
    if(pf==NULL)
    {
        printf("\n No se pudo acceder");
        return 0;
    }
    printf("\n Archivo creado! ");
    fclose(pf);
    return 1;
}

////////////////

int AbrirArchivo(t_registro *reg)
{
    FILE *pf;

    pf=fopen("archivo.dat","r+b");
    if(pf==NULL)
    {
        printf("\n No se pudo acceder");
        return 0;
    }
    printf("\n Se abrio el archivo!");
    return 1;

}

///////////////

int AgregarRegistro(t_registro *reg)
{
    FILE *pf;

   pf=fopen("archivo.dat","a+b");
    if(pf==NULL)
    {
        printf("\n No se pudo acceder");
        return 0;
    }
    printf("\n Ingrese datos a agregar..");

    printf("\n Nro Registro \t Nombre \t Apellido \t Edad \t \n");
    fflush(stdin);
    scanf("%d %s %s %d", &reg->nreg,reg->nombre,reg->apellido,&reg->edad);
    fflush(stdin);
    fwrite(&reg,sizeof(t_registro),1,pf);
    fclose(pf);
   return 1;

}
///////////////

int MostrarRegistro(t_registro const *reg)
{
    FILE *pf;

    pf=fopen("archivo.dat","rb");
    if(pf==NULL)
    {
        printf("\n No se pudo acceder");
        return 0;
    }
    printf("\n Datos hasta el momento \n");
    printf("\n Nro Registro \t Nombre \t Apellido \t Edad \t \n \n");
    fread(&reg,sizeof(t_registro),1,pf);
    fflush(stdin);
    while(!feof(pf))
    {

        printf("\n %d \t\t %s \t\t %s \t %d",reg->nreg,reg->nombre,reg->apellido,reg->edad);
        fread(&reg,sizeof(t_registro),1,pf);
        fflush(stdin);
    }
    fclose(pf);
    return 1;
}



Si me pudieran señalar los errores, se los agradecería muchisimo
Saludos!