ejemplo de archivos en C
By: a guest | Mar 17th, 2010 | Syntax:
C++ | Size: 1.68 KB | Hits: 160 | Expires: Never
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
struct comedor
{
char cedula[9];
char nombre[20];
char direccion[30];
char fecha[9];
int valido;
}gente;
void ingresar();
void mostrar();
FILE *punt_arch;
void main()
{
int opc=0;
while(opc!=3)
{
fflush(stdin);
clrscr();
printf("\t\t***MENU***\n\t1.-Ingresar Usuario\n\t2.-Mostrar todos los usuarios\n\t3.-Salir\n\tIngrese su opcion: ");
scanf("%d",&opc);
fflush(stdin);
switch(opc)
{
case 1:
ingresar();
break;
case 2:
mostrar();
break;
}
}
clrscr();
printf("\t\tPOWERED BY ANDRES");
getch();
}
void ingresar()
{
punt_arch=fopen("comedor.bin","a+b");
long int ced;
printf("\n\tingrese la cedula: ");
gets(gente.cedula);
printf("\n\tingrese el nombre: ");
gets(gente.nombre);
printf("\n\tingrese la direccion: ");
gets(gente.direccion);
printf("\n\tIngrese la fecha (DDMMAAAA): ");
gets(gente.fecha);
gente.valido=1;
ced=atol(gente.cedula);
fseek(punt_arch,(ced*sizeof(comedor)),SEEK_END);
fwrite(&gente,sizeof(struct comedor),1,punt_arch);
fclose(punt_arch);
fflush(stdin);
}
void mostrar()
{
punt_arch=fopen("comedor.bin","r+b");
int vacio=0;
fflush(stdin);
fseek(punt_arch,0,SEEK_SET);
while(fread(&gente, sizeof(struct comedor), 1, punt_arch))
{
if(gente.valido==1)
{
printf("\n\tCedula: ");
puts(gente.cedula);
printf("\n\tNombre: ");
puts(gente.nombre);
printf("\n\tDireccion: ");
puts(gente.direccion);
printf("\n\tFecha de ingreso al comedor: ");
puts(gente.fecha);
getch();
printf("\t.....................................");
vacio=1;
}
}
if(vacio==0)
{
printf("\tNo hay nadie registrado");
getch();
}
fclose(punt_arch);
}