Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Añadir registro al fichero binario

Estas en el tema de Añadir registro al fichero binario en el foro de C/C++ en Foros del Web. Hola amigos me pueden ayudar a resolver mi problema todo me lo hace bien menos la funcion aniadir... muchas gracias de antemano #include <stdio.h> #include ...
  #1 (permalink)  
Antiguo 27/05/2010, 13:22
 
Fecha de Ingreso: mayo-2010
Mensajes: 2
Antigüedad: 14 años
Puntos: 0
Añadir registro al fichero binario

Hola amigos me pueden ayudar a resolver mi problema todo me lo hace bien menos la funcion aniadir... muchas gracias de antemano

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define tam 2
struct clase
{
char nombre[20];
char apellido[20];
float evaluacion1;
float evaluacion2;
float evaluacion3;
};

void introducir(struct clase notas[]);
void escribir (struct clase notas);
void leerfichero(struct clase notas[]);
void modificar (struct clase notas[]);
void aniadir (struct clase notas);

int main()
{
struct clase notas[tam];
introducir(notas);
leerfichero(notas);
modificar(notas);
leerfichero(notas);
aniadir(notas[tam]);
}

void introducir(struct clase notas[])
{
FILE *fichero;
fichero = fopen("registro.txt", "wb");
if (fichero == NULL)
{
printf("no hay fichero");
}
for (int i = 0;i < tam;i++)
{
fflush(stdin);
printf("introduce nombre: ");
gets(notas[i].nombre);
printf("introduce apellidos: ");
gets(notas[i].apellido);
printf("introduce notas de la evaluacion1: ");
scanf("%f", &notas[i].evaluacion1);
printf("\nintroduce notas de la evaluacion2: ");
scanf("%f", &notas[i].evaluacion2);
printf("\nintroduce notas de la evaluacion3: ");
scanf("%f", &notas[i].evaluacion3);
fwrite(&notas[i], sizeof(struct clase), 1, fichero);
}
fclose(fichero);
printf("\n los datos han sido insertados en el fichero");
}

void escribir (struct clase notas)
{
printf("\n%s \t%s\t \t%.2f \t%.2f \t%.2f", notas.nombre, notas.apellido, notas.evaluacion1, notas.evaluacion2, notas.evaluacion3);
}
void leerfichero(struct clase notas[])
{
int i;
FILE *fichero;
fichero = fopen("registro.txt", "rb");
if (fichero == NULL)
{
printf("no hay fichero");
}
printf("\nNombre \tApellido \t1Ev \t2Ev \t3Ev");
for (i = 0;i < tam;i++)
{
fread(&notas[i], sizeof(struct clase), 1, fichero);
escribir(notas[i]);
}
printf("\n Los datos del fichero han sido leidos correctamente");
fclose(fichero);
}
void modificar (struct clase notas[])
{
int i, aux;
char nombre[20];
FILE *fichero;
fichero = fopen("registro.txt", "r+b");
if (fichero == NULL)
{
printf("no hay fichero");
}
fflush(stdin);
printf ("\nIntroduce alumno a modificar: ");
gets(nombre);
for (i = 0;i < tam;i++)
{
aux = strcmp(notas[i].nombre, nombre);
fread(&notas[i], sizeof(struct clase), 1, fichero);
if (aux == 0)
{
printf("Encontrado: \n%s \t%s\t \t%.2f \t%.2f \t%.2f", notas[i].nombre, notas[i].apellido, notas[i].evaluacion1, notas[i].evaluacion2, notas[i].evaluacion3);
fseek (fichero, -(sizeof(struct clase)), SEEK_CUR);
fflush(stdin);
printf("\nintroduce nombre: ");
gets(notas[i].nombre);
printf("introduce apellidos: ");
gets(notas[i].apellido);
printf("introduce notas de la evaluacion1: ");
scanf("%f", &notas[i].evaluacion1);
printf("\nintroduce notas de la evaluacion2: ");
scanf("%f", &notas[i].evaluacion2);
printf("\nintroduce notas de la evaluacion3: ");
scanf("%f", &notas[i].evaluacion3);
fwrite(&notas[i], sizeof(struct clase), 1, fichero);
}
}
fclose(fichero);
printf ("\nDatos Modificados en el fichero correctamente \n");
}

void aniadir (struct clase notas)
{

FILE *fichero;
fichero = fopen("registro.txt", "ab");
if (fichero == NULL)
{
printf("no hay fichero");
}

printf("\nDatos a incluir: ");
fflush(stdin);
printf("\nintroduce nombre: ");
gets(notas.nombre);
printf("introduce apellidos: ");
gets(notas.apellido);
printf("introduce notas de la evaluacion1: ");
scanf("%f", &notas.evaluacion1);
printf("\nintroduce notas de la evaluacion2: ");
scanf("%f", &notas.evaluacion2);
printf("\nintroduce notas de la evaluacion3: ");
scanf("%f", &notas.evaluacion3);
fseek (fichero, 0, SEEK_END);
fwrite(&notas, sizeof(struct clase), 1, fichero);
printf ("\nDatos Introducidos en el fichero correctamente \n");
fclose(fichero);
}
  #2 (permalink)  
Antiguo 28/05/2010, 05:18
 
Fecha de Ingreso: mayo-2010
Mensajes: 2
Antigüedad: 14 años
Puntos: 0
Respuesta: Añadir registro al fichero binario

se que es cambiando la constante tam pero la poniendola como variable global y con un contador tampoco me lo muestra (es decir si me lo hace pero tengo que cambiar manualmente la constante)

Etiquetas: añadir, binario, fichero, registro
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:44.