Dev - C++ - dev c++ ayuda!!!

 
Vista:

dev c++ ayuda!!!

Publicado por cristina (2 intervenciones) el 15/06/2011 23:33:27
ayurme agregar promedio de edad.



/*
* Ficheros01.c
*
*/

/* Archivos cabecera */
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>

// Prototipos
void menu();
void CrearFichero(FILE *Fichero);
void InsertarDatos(FILE *Fichero);
void VerDatos(FILE *Fichero);

// Definición de una estructura
struct sRegistro {
char Nombre[25];
int Edad;
float Sueldo;
} registro;

// Programa principal
int main(int argc, char** argv)
{
int opcion;
int exit = 0;
FILE *fichero;

while (!exit)
{
menu();
printf("\n\nOPCION: ");
scanf("%d", &opcion);

switch(opcion)
{
case 1:
CrearFichero(fichero);
break;
case 2:
InsertarDatos(fichero);
break;
case 3:
VerDatos(fichero);
break;
case 4:
exit = 1;
break;
default:
printf("\nopcion no valida");
}
}

return 0;
}

// Presenta el menú en la pantalla
void menu()
{
printf("\n\nARCHIVOS EN DISCO\n");
printf("-----------------");
printf("\nMENU:");
printf("\n\t1. Crear fichero");
printf("\n\t2. Insertar datos");
printf("\n\t3. Ver datos");
printf("\n\t4. Salir");

return;
}

// Función que crea un archivo en disco
void CrearFichero(FILE *Fichero)
{
Fichero = fopen("fichero", "r");

if(!Fichero)
{
Fichero = fopen("fichero", "w");
printf("\nArchivo creado!\n");
}
else
{
printf("\nEl fichero ya existe!\n");
}

fclose (Fichero);

return;
}

// Función que inserta datos en el archivo
void InsertarDatos(FILE *Fichero)
{
Fichero = fopen("fichero", "a+");

if(Fichero == NULL)
{
printf("\nFichero no existe! \nPor favor creelo");
return;
}

printf("\nDigita el nombre: ");
scanf("%s", registro.Nombre);

printf("\nDigita la edad: ");
scanf("%d", &registro.Edad);

printf("\nDigita el sueldo: ");
scanf("%f", &registro.Sueldo);

fwrite(&registro, sizeof(struct sRegistro), 1, Fichero);

fclose(Fichero);

return;
}

// Función que presenta información en la pantalla
void VerDatos(FILE *Fichero)
{
int numero = 1;

Fichero = fopen("fichero", "r");

if(Fichero == NULL)
{
printf("\nFichero no existe! \nPor favor creelo");
return;
}

fread(&registro, sizeof(struct sRegistro), 1, Fichero);

printf("\nNumero \tNombre \tEdad \tSueldo");

while(!feof(Fichero))
{
printf("\n%d \t%s \t%d \t%.2f", numero, registro.Nombre,
registro.Edad, registro.Sueldo);
fread(&registro, sizeof(struct sRegistro), 1, Fichero);
numero++;
}

fclose(Fichero);

return;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder