Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/11/2014, 16:48
Avatar de leosansan
leosansan
 
Fecha de Ingreso: mayo-2012
Ubicación: GRAN CANARIA
Mensajes: 194
Antigüedad: 12 años
Puntos: 49
Respuesta: Escribir y abrir archivo de texto con C

Cita:
Iniciado por dario2494 Ver Mensaje
Hola a todos estoy realizando un trabajo para la facu y me surgió un inconveniente. Si bien el programa funciona tengo un problema. Yo pido por teclado cada linea a grabarse en el archivo, el problema es que cuando con otra función muestro el contenido del archivo se muestran las todas lineas juntas..como si fuera un solo renglón.
Creo que lo siguiente te ayudará:

Código C++:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_LINEA 100
  5. void AbreBlocdeNotas(void){
  6.   FILE *Txt;
  7.     Txt=fopen("hola.txt","r");
  8.     if (Txt==NULL){
  9.         printf("No se ha podido abrir el archivo \r\n");
  10.         exit(1);
  11.     }
  12.     char CadenaLeer[MAX_LINEA]="";
  13.     while ( fgets(CadenaLeer, MAX_LINEA, Txt)){
  14.         puts(CadenaLeer);
  15.     }
  16.     fclose(Txt);
  17. }
  18. void CreaBlocdeNotas(void){
  19.     FILE *Txt;
  20.     Txt=fopen("hola.txt","w");
  21.     char Cadena[MAX_LINEA]="";
  22.     if (Txt==NULL){
  23.         printf("No se ha podido abrir el archivo \r\n");
  24.         exit(1);
  25.     }
  26.    
  27.     do {
  28.       printf("Ingrese una linea a grabar en arcivo: \r\n");
  29.       gets(Cadena);
  30.       if ( strcmp(Cadena,"FIN")==0 )
  31.         break ;
  32.     fprintf (Txt,"%s%s", Cadena,"\n" );
  33.     }while ( 1 ) ;
  34.  
  35.     fclose(Txt);
  36.    
  37. }
  38. int main() {
  39.     CreaBlocdeNotas();
  40.     AbreBlocdeNotas();
  41.     return 0;
  42. }

Ya sé que no es la solución más elegante pero cumple su cometido.

¡¡¡Saluditos!!!