Ver Mensaje Individual
  #7 (permalink)  
Antiguo 08/06/2010, 05:46
minette1988
 
Fecha de Ingreso: febrero-2010
Mensajes: 258
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: sustituir el salto de línea por una coma

He modificado el ejercicio que consiste en abrir el fichero binario e introducir datos, luego sustituyo el salto de linea por coma y luego muestro los registros por pantalla:

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LEE_CHAR(c)\
  6.         c=getchar();\
  7.         while((c=='\n') || (c==' ') || (c=='\t'))\
  8.             c=getchar();
  9.  
  10. #define LEE_CAD(c,n) { int i=0; \
  11.                      c[i]=getchar();\
  12.              while((c[i]=='\n') || (c[i]=='\t')) c[i]=getchar();\
  13.              while ((c[i]!='\n') && (i < (n-1))){ \
  14.                 i++;\
  15.             c[i]=getchar(); }\
  16.              c[i]='\0'; }
  17.  
  18. #define TAM_NOMBRE 100
  19. #define TAM_DIR 100
  20.  
  21. struct cliente{
  22.     char nombre[TAM_NOMBRE];
  23.     char direccion[TAM_DIR];
  24.         double deuda;
  25. };
  26. int main(){
  27.     void crear_fich();
  28.         int cambiar_salto_por_coma();
  29.         void mostrar_fich();
  30.         char op;
  31.     FILE *pf; //Puntero a fichero
  32.  
  33.         /* Abre el fichero para trabajar con él en Lectura / Escritura */
  34.         if((pf = fopen("datos.dat", "wb+")) == NULL) {
  35.            /* Si no existe, ejecuta el módulo que lo cree */
  36.        crear_fich();
  37.        /* Una vez creado lo habre en lectura/escritura */
  38.            if((pf = fopen("datos.dat", "rb+")) == NULL) {
  39.         perror("Fichero no accesible");
  40.                 exit(1);
  41.        }
  42.         }
  43.  
  44.     do{
  45.         printf("MENÚ\n----\n");
  46.         printf("1). Crear fichero.\n");
  47.                 printf("2). Cambiar salto de linea por coma.\n");
  48.                 printf("3). Mostrar fichero.\n");
  49.         printf("0). Salir\n\nOpción(0-3): ");
  50.         do{
  51.             LEE_CHAR(op);
  52.         }while( (op <'0') || (op >'3') );
  53.  
  54.         switch(op){
  55.                 case '1':
  56.                         crear_fich(pf);
  57.                         /* Una vez creado lo abre en lectura/escritura */
  58.                         if((pf = fopen("datos.dat", "rb+")) == NULL) {
  59.                         perror("Fichero no accesible");
  60.                                 exit(2);
  61.                         }
  62.                     break;
  63.                                 case '2': cambiar_salto_por_coma(pf);
  64.                                         break;
  65.                                 case '3': mostrar_fich(pf);
  66.                                         break;
  67.          } //switch
  68.         }while(op!='0');
  69.     if(fclose(pf) == EOF)
  70.            printf("Error al cerrar el fichero.\n");
  71.         exit(0);
  72. }//main()
  73. ////////////////////////////////////////////////////////////////////////
  74. void crear_fich(FILE* pf) {
  75.          struct cliente cli;
  76.          char otro;
  77.  
  78.      if((pf = fopen("datos.dat", "wb")) == NULL) {
  79.         perror("Al crear el fichero de datos");
  80.                 exit(4);
  81.          }
  82.          
  83.          /* Lectura de los ciclistas */
  84.          do {
  85.        
  86.         cli.deuda = 0.0;
  87.                 printf("Nombre: "); LEE_CAD(cli.nombre, TAM_NOMBRE);
  88.                 printf("Dirección: "); LEE_CAD(cli.direccion, TAM_DIR);
  89.                 printf("Deuda: "); scanf("%lf", &cli.deuda);
  90.                 /*Escribe el cliente en el fichero */
  91.                 fwrite(&cli, sizeof(struct cliente), 1, pf);
  92.         printf("¿Otro? (s/n) ");
  93.                 LEE_CHAR(otro);
  94.     }while((otro=='s') || (otro=='S'));
  95.     if(fclose(pf) == EOF){
  96.         printf("Error al cerrar el fichero, compruebe si hay información.\n"); 
  97.                 exit(5);
  98.         }
  99. } /* crear_fich()*/
  100. ///////////////////////////////
  101. int cambiar_salto_por_coma(FILE *pf){
  102.  
  103.   char letra;
  104.  
  105.   // Abro el fichero
  106.   if((pf=fopen("datos.dat","rb"))==NULL){
  107.       printf("Error al abrir el fichero.\n");
  108.       return 1;
  109.   }
  110.   else{
  111.      letra = getc(pf);
  112.      while ( !feof(pf) ){ // Mientras no sea final de fichero
  113.      if(letra=='\n'){
  114.         letra=',';
  115.      }
  116.      printf("%c",letra);  
  117.      letra = getc(pf);
  118.     }
  119.   // Cerrar fichero
  120.   fclose(pf);
  121.   getchar();
  122.   return 0;
  123.   }
  124. }
  125. //////////////////////////////////
  126. void mostrar_fich(FILE *pf){
  127.  
  128.        struct cliente cli;
  129.        
  130.        rewind(pf);
  131.        fread(&cli,sizeof(struct cliente),1,pf);
  132.        while(!feof(pf)){
  133.           printf("%s %s %.2lf\t\n\n",cli.nombre,cli.direccion,cli.deuda);
  134.           fread(&cli,sizeof(struct cliente),1,pf);
  135.        }
  136.        if(fclose(pf) == EOF){
  137.         printf("Error al cerrar el fichero, compruebe si hay información\n");  
  138.                 exit(1);
  139.         }
  140. }

Cuando pido que me muestre el fichero, me salen los registros, pero no me ha sustituido el \n por la coma, ¿por qué?
Me sale esto:
pepe aaa 41.14
ana qqq 65.32

pero sin la coma que tiene que separar cada registro que es lo que a mi me interesa, ¿por qué no sale la coma?