[C con Clase] Ayuda Necesito Guardar los datos ingresados de un programa en .txt manejo de archivos

Carlos Falgueras García fark.zano en gmail.com
Vie Jul 9 12:27:25 CEST 2010


Aquí te dejo una recopilación de algunos tutoriales:

- http://c.conclase.net/curso/index.php?cap=039#inicio ( C++ )
 <http://c.conclase.net/curso/index.php?cap=039#inicio>-
http://www.elrincondelc.com/cursoc/cursoc.html ( Capítulos 19-21, C )
- http://www.aprendeaprogramar.com/mod/resource/view.php?id=322 ( C )
- http://www.cplusplus.com/reference/iostream/ ( C++ , todo sobre la
librería iostream que necesitas para manipular ficheros [con ejemplos] )

El 8 de junio de 2010 09:49, Ismael Camarero <icamarero98 en hotmail.com>escribió:

>
> Busca en**
> **
> http://programandoenc.webcindario.com
>
>
>
>
> ------------------------------
> From: jclaudio_4 en hotmail.com
> To: cconclase en listas.conclase.net
> Date: Sun, 6 Jun 2010 23:58:06 -0600
> Subject: [C con Clase] Ayuda Necesito Guardar los datos ingresados de un
> programa en .txt manejo de archivos
>
>
>
>  /*
>  PROGRAMA QUE PERMITE REALIZAR EL MANEJO DE UNA BIBLIOTECA, UTILIZA MANEJO
> DE MEMORIA DINAMICA
>  SE PUEDE ADICIONAR LIBROS, ELIMINAR, MODIFICAR Y ORDENAMIENTO
>
>  */
>
> Utilizo este medio para poder pedir de su ayuda, porfavor si alquien me
> puede ayudar, con este problema, se lo agradesco, aqui les dejo los datos.
>
> Este es el programa que necesito que guarde la informacion en un documento
> de .txt, tambien que realize busqueda, eliminacion pero del .txt.
>
> La verdad no he podido pero ya intente de varias formas con FILE *archivo;
> ofstream para crearlo ifstream para leerlo pero la verda no he podido, le
> pido de favos
> si alguien me puede ayudar se lo agradeceria vastante, muchisimo.
>
>
> #include <conio.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <iostream.h>
> #include <string.h>
> #include <dos.h>
> #include <windows.h>
>
> #include<fstream.h>
> //#include<graphics.h>
>
> #define g gotoxy
>
> char *capturar(int longitud, int tipo); //FUNCION DE VALIDACION( CUANDO SE
> QUIERE QUE SE DIGITE LO QUE ES)
> void iniciar();
> void adicionar();
> void listar();
> void eliminar();
> void ordenar();
> void modificar();
> void load();  //EMULA UNA BARRA DE PROGRESO
>
> struct libro
> {
>  int cod;
>  char tit[30],aut[30],edi[30],gen[30],fec[30];
> };
> //aux **bib=NULL;
> libro **bib=NULL;libro **aux=NULL;    //libro **aux2=NULL;   DECLARACION DE
> PUNTEROS
> int tam=0;    //VARIABLE QUE ME DETERMINA EL NUMERO DE LIBROS
> void  main()
> {
>
>  ;
>  clrscr();
>      //window(1,1,80,25);textbackground(1);clrscr();window(3,2,78,24);
>     // textbackground(0);textcolor(15);clrscr();
>
>     char op;
>
>     do{    //CREACION DEL MENU
>         g(60,23);
>         //textcolor(BLINK+RED);
>         puts("'ESC' PARA SALIR");
>         //textattr(30);
>         gotoxy(28,8);
>         printf("Menu Principal\n");
>         gotoxy(28,9);
>         //textattr(14);
>         printf("1");
>         //textattr(7);
>         printf(" - INGRESAR\n");
>         gotoxy(28,10);
>         //textattr(14);
>         printf("2");
>         //textattr(7);
>         printf(" - LISTADOS\n");
>         gotoxy(28,11);
>         //textattr(14);
>         printf("3");
>         //textattr(7);
>         printf(" - ELIMINAR\n");
>         gotoxy(28,12);
>         //textattr(14);
>         printf("4");
>         //textattr(7);
>         printf(" - ORDENAR\n");
>         gotoxy(28,13);
>         //textattr(14);
>         printf("5");
>         //textattr(7);
>         printf(" - MODIFICAR");
>         gotoxy(28,15);
>         //textattr(15);
>         printf("Opcion");
>         //textattr(RED+BLINK);
>         printf("[ ]\b\b");
>
>         op=getch();
>             switch(op)
>         {
>             case '1':
>                 clrscr();
>                 adicionar(Fichero);
>                 break;
>             case '2':
>                 clrscr();
>                 listar( );
>                 break;
>             case '3':
>                 clrscr();
>                 eliminar();
>                 break;
>             case '4':
>                 clrscr();
>                 ordenar();
>                 break;
>             case '5':
>                 clrscr();
>                 modificar();
>                                 break;
>
>         }
>
>
>         } while (op!=27);
>     //vaciar();
>
>     if(bib) bib=NULL; delete bib;
>     if (aux)  aux=NULL; delete aux;
>
>
>
> }
> void iniciar()  //PIDO MEMORIA
> {bib =new libro*[tam];
>
> }
>
>
>
> void adicionar()   //FUNCION PARA ADICIONAR LIBROS
> {
>
>
>
>     if(tam==0){iniciar(Fichero); bib[tam]=new libro;}
>      else
>      {
>       aux =new libro*[tam+1];
>       aux[tam]=new libro;
>             for(int i=0;i<tam;i++)
>              {aux[i]=bib[i];
>              }
>
>             if(bib)    bib=NULL; delete bib;
>             bib=aux;
>             aux=NULL;  delete aux;
>
>
>          }
>          clrscr();
>
>
>
>          g(4,4);cout<<"CODIGO:  ";int
> w=atoi(capturar(30,0));bib[tam]->cod=w;
>          g(4,5);cout<<"TITULO:  ";strcpy(bib[tam]->tit,capturar(30,1));
>          g(4,6);cout<<"AUTOR :  ";strcpy(bib[tam]->aut,capturar(30,1));
>          g(4,7);cout<<"EDITORIAL:  ";strcpy(bib[tam]->edi,capturar(30,1));
>          g(4,8);cout<<"GENERO:  ";strcpy(bib[tam]->gen,capturar(30,1));
>          g(4,9);cout<<"FECHA :
> ";strcpy(bib[tam]->fec,capturar(30,2));tam++; clrscr();
>
>
>
> }
>
> void listar() //FUNCION PARA LISTAR LOS LIBROS
> {clrscr();int y=2;
>  for(int i=0;i<tam;i++)
>     {if(bib[i])
>      {
>       g(1,1);
>       //textcolor(GREEN);
>       puts("CODIGO TITULO           AUTOR           EDITORIAL
> GENERO      FECHA  ");
>       g(1,y);cout<<bib[i]->cod;
>       g(8,y);cout<<bib[i]->tit;
>       g(25,y);cout<<bib[i]->aut;
>       g(41,y);cout<<bib[i]->edi;
>       g(56,y);cout<<bib[i]->gen;
>       g(66,y);cout<<bib[i]->fec;
>       y++;
>      }
>     }
>     getch();clrscr();
> }
> void eliminar() //FUNCION QUE ELIMINA LIBROS
> {clrscr();int AUX;
>  //aux2=new libro*[tam];
>  g(4,4);puts("CàDIGO: ");g(14,4);int z=atoi(capturar(30,0));AUX=z;
>  for(int i=0;i<tam&&AUX!=bib[i]->cod;i++);
>     if(i==tam){g(4,20);puts("NO EXISTE EL CODIGO;");getch();clrscr();}
>     else
>      {bib[i]=NULL;g(4,20);puts("LIBRO ELIMINADO"); getch();
>       aux=new libro*[tam-1]; clrscr();
>       aux[tam]=new libro;
>       for(int i=0,j=0;i<tam;i++)
>        {if(bib[i]){aux[j]=bib[i];j++;}
>        }
>      // if(bib[i]){ bib[i]=NULL; delete bib[i]; }
>       if(bib) {bib=NULL;delete bib;}
>       bib=aux;getch();tam=j;
>       //if(aux) {aux=NULL;  delete aux;}
>
>      }
> }
> void ordenar() //FUNCION QUE ORDENA LOS LIBROS POR AUTOR
> {libro *aux2;
>  int com=0,i;
>  if(tam!=0)
>  {
>  for(int i=0;i<tam;i++)
>     {for(int j=i;j<tam;j++)
>         {com=strcmp(bib[j]->aut,bib[i]->aut);
>          if(com<0)
>             {aux2=bib[i];
>              bib[i]=bib[j];
>              bib[j]=aux2;
>             }
>         }
>     }
>
>
>   load();clrscr();//g(4,20);puts("DATOS ORDENADOS ...");getch();clrscr();
>   }
>   else{g(4,20);puts("NO HAY DATOS"); getch();clrscr();}
> }
>
> void modificar()  //FUNCION PARA MODIFICAR DATOS DE LOS LIBROS EXCEPTO
> CODIGO
> {
>  clrscr();
>  int auz;
>  g(4,3);puts("CàDIGO:");g(12,3);int x=atoi(capturar(30,0));auz=x; g(4,3);
> clreol();
>  for(int i=0;i<tam&&auz!=bib[i]->cod;i++);
>  if(i==tam){g(4,20);puts("NO EXISTE EL CODIGO;");getch();clrscr(); }
>  else
>  {
>
>
>          g(4,4);cout<<"CODIGO:  ";cout<<bib[i]->cod;
>          g(4,5);cout<<"TITULO:  ";cout<<bib[i]->tit;
>          g(4,6);cout<<"AUTOR :  ";cout<<bib[i]->aut;
>          g(4,7);cout<<"EDITORIAL:";cout<<bib[i]->edi;
>          g(4,8);cout<<"GENERO:  ";cout<<bib[i]->gen;
>          g(4,9);cout<<"FECHA :  ";cout<<bib[i]->fec;
>
>
>          g(25,15);puts("¨Qu‚ dato desea modificar?");
>          g(60,23);
>          //textcolor(BLINK+RED);
>          puts("0 PARA SALIR");
>          g(4,16);cout<<"1 - TITULO  ";
>          g(4,17);cout<<"2 - AUTOR   ";
>          g(4,18);cout<<"3 - EDITORIAL";
>          g(4,19);cout<<"4 - GENERO  ";
>          g(4,20);cout<<"5 - FECHA   ";
>          g(4,23);printf("Opcion");
>          printf("[ ]\b\b");
>          char op2=getch();     char aus[30];
>          switch(op2)
>          {
>           case '1':
>                 clrscr();
>                 g(4,4);puts("NUEVO
> TÖTULO:");g(15,4);strcpy(aus,capturar(30,1));
>                 strcpy(bib[i]->tit,aus);
>                 break;
>
>           case '2':
>                 clrscr();
>                 g(4,4);puts("NUEVO
> AUTOR:");g(18,4);strcpy(aus,capturar(30,1));
>                 strcpy(bib[i]->aut,aus);
>                 break;
>           case '3':
>                 clrscr();
>                 g(4,4);puts("NUEVO
> EDITORIAL:");g(20,4);strcpy(aus,capturar(30,1));
>                 strcpy(bib[i]->edi,aus);
>                 break;
>           case '4':
>                 clrscr();
>                 g(4,4);puts("NUEVO
> GENERO:");g(18,4);strcpy(aus,capturar(30,1));
>                 strcpy(bib[i]->gen,aus);
>                 break;
>           case '5':
>                 clrscr();
>                 g(4,4);puts("NUEVA
> FECHA:");g(18,4);strcpy(aus,capturar(30,2));
>                 strcpy(bib[i]->fec,aus);
>                 break;
>
>
>   }
> // else{ puts("NO REGISTRADO"); getch(); }
>
>   clrscr();
>  }
> }
> //FUNCION DE VALIDACION
> char *capturar(int longitud,int tipo)//tipo 0=NUMEROS,1=LETRAS,2=NUMEROS Y
> LETRAS Y (-)
>   {
>   char aux[81],i=0;
>   unsigned char tecla;
>   while(tecla!=13)
>     { tecla=getch();
>       if(tecla==8)
>        { if( i>0)
>       { gotoxy(wherex()-1,wherey());
>         cout<<" ";
>         gotoxy(wherex()-1,wherey());
>         i--;
>        }
>     }
>
>
>       if(tecla==0) getch();
>       else
>     { if(i<longitud-1)
>        {switch(tipo)
>           {case 1: if ((tecla>='A'&& tecla<='Z') || (tecla>='a' &&
> tecla<='z') || (tecla==' '))
>              {
>
>
>                cout<<tecla;
>                aux[i++]=tecla;
>              }
>              break;
>            case 2: if ((tecla>='0'&& tecla<='9') || (tecla>='A'&&
> tecla<='Z') || (tecla>='a' && tecla<='z') || (tecla==' ') || (tecla=='-'))
>              { cout<<tecla;
>                aux[i++]=tecla;
>              }
>              break;
>            case 0:   if((tecla>='0'&& tecla<='9'))
>              {cout<<tecla;
>               aux[i++]=tecla;
>              }
>               break;
>            }//switch
>        }//if
>     }//else
>       }//while
>    aux[i]=0;
>    cout<<"\n";
>    return aux;
>    }
>
>
> void load()
> {
>
>     clrscr();
>     int p=-3,x;
>     //textcolor(RED);
>     g(26,5);printf("    ORDENANDO ...");
>     gotoxy(16,6);printf("0%ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ");
>     //textcolor(LIGHTGREEN);
>     for (x=19;x<=57;x++) {
>         gotoxy(x,6);printf("Ü");if (p==111){p=100;}else{p=p+3;}
>         gotoxy(59,6);printf("%d%",p);
>
>     }
>     printf("\n\n\t\t               Proceso terminado\n\n\t\t\t   Presione
> cualquier tecla");getche();
>
>
> }
>
> ------------------------------
> Explore the seven wonders of the world Learn more!<http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE>
> ------------------------------
> ¿Un navegador seguro buscando estás? ¡Protegete ya en
> www.ayudartepodria.com! <http://www.ayudartepodria.com>
>
> _______________________________________________
> Lista de correo Cconclase Cconclase en listas.conclase.net
> http://listas.conclase.net/mailman/listinfo/cconclase_listas.conclase.net
> Bajas: http://listas.conclase.net/index.php?gid=2&mnu=FAQ
>
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.conclase.net/pipermail/cconclase_listas.conclase.net/attachments/20100709/54aa7914/attachment.html>


Más información sobre la lista de distribución Cconclase