Adsence

martes, 11 de enero de 2011

Generar-Archivo-de-Texto

Leave a Comment
[caption id="attachment_660" align="aligncenter" width="677"]Crear archivo de texto en C Crear archivo de texto en C[/caption]

Este archivo lo copie desde aqui le doy todo el credito a su creador Julio César Brizuela <brizuela@linux-qxlk>



[cpp]

/*
* FicheroCompleto.c
*
* Copyright 2009 Julio César Brizuela < brizuela@linux-qxlk >
*
*/

#include < stdio.h >

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

struct sRegistro {
char Nombre[25];
int Edad;
float Sueldo;
}registro;

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

while (!exit)
{
menu();
printf("\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;
}

void menu()
{
printf("\nMenu:");
printf("\n\t1. Crear fichero");
printf("\n\t2. Insertar datos");
printf("\n\t3. Ver datos");
printf("\n\t4. Salir");
}

void CrearFichero(FILE *Fichero)
{
Fichero = fopen("fichero", "r");

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

fclose (Fichero);

return;
}

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",®istro;.Nombre);

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

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

fwrite(®istro;, sizeof(registro), 1, Fichero);

fclose(Fichero);

return;
}

void VerDatos(FILE *Fichero)
{
int numero = 1;

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

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

fread(®istro;, sizeof(registro), 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(®istro;, sizeof(registro), 1, Fichero);
numero++;
}

fclose(Fichero);

return;
}

[/cpp]

0 comentarios :