lunes, 16 de enero de 2012

Ficheros 2


#include <stdio.h>
#include <stdlib.h>

int main()
{
      int c,i;
      char s[100];
      FILE *pf;

      /* abrimos un fichero donde escribimos
         con fputs() y leemos con fgets() */

      pf=fopen("hola.txt","w+");

      fputs("hola pepe\n",pf);          /* escribimos en el fichero */
      fputs("hasta luego\n",pf);
      rewind(pf);

      system ("pause");

      printf("\nLEEMOS FICHERO:\n");
      fgets(s,100,pf);                  /* leemos del fichero */
      puts(s);
      fgets(s,20,pf);
      puts(s);
      if(feof(pf)) printf("error_1\n");
      system("pause");

      /* si sigo leyendo hay error */
      printf("\nSI SEGUIMOS LEYENDO MAS ALLA DE EOF --> ERROR!\n");
      fgets(s,100,pf);
      if(feof(pf)) printf("error_2\n");
      puts(s);
      fclose(pf);

      /* reabrimos y leemos caracter a caracter  */
      pf=fopen("hola.txt","r+");
      printf("LEEMOS CARACTER A CARACTER: \n");
      while(feof(pf)==0)
        {
         c=getc(pf);
         putchar(c);
        }
     putchar('\n');
     fclose(pf);
     system("pause");

     printf("\n FINALMENTE BORRAMOS EL FICHERO.\n");
     remove("hola.txt");

     printf("\n\n");
     system("PAUSE");
     return 0;

}

No hay comentarios:

Publicar un comentario