Fich: movim cursor

16 fich movim cursor.html
/* Manejo de ficheros: movimientos de cursor en un fichero */
#include <stdio.h>
 
int main ()
{
   char name [20];
   int age, length;
   FILE *fp;
   fp = fopen ("test.txt","w");
   fprintf (fp, "%s %d", "Fresh2refresh", 1); //Fresh2refresh + espacio + 1 =15 caracteres
   length = ftell(fp); // Posicion del cursor del fichero
   printf ("Total number of characters in file is %d\n", length);
   
//   fclose (fp);                //fclose+fopen == freopen
//   fp = fopen ("test.txt","r");
   freopen ("test.txt","r", fp);  // == fclose + fopen
   rewind (fp); // mueve el cursor al principio del fichero
   length = ftell(fp); // Posicion del cursor del fichero
   printf ("Total number of characters in file is %d\n", length);
   
   fscanf (fp, "%s", name);
   fscanf (fp, "%d", &age);
   printf ("Name2: %s \n Age: %d \n",name,age);
   length = ftell(fp); // Posicion del cursor del fichero
   printf ("Total number of characters in file is %d\n", length);
   
   fseek(fp, 0, SEEK_SET);
   //rewind (fp); // mueve el cursor al principio del fichero
   length = ftell(fp); // Posicion del cursor del fichero
   printf ("Total number of characters in file is %d\n", length);
   fscanf (fp, "%s", name);
   fscanf (fp, "%d", &age);
   length = ftell(fp); // Posicion del cursor del fichero
   printf ("Total number of characters in file is %d\n", length);
   
   fclose (fp);
   printf ("Name: %s \n Age: %d \n",name,age);
   length = ftell(fp); // Posicion del cursor del fichero
   printf ("Total number of characters in file is %d\n", length);
   return 0;
}


No hay comentarios:

Publicar un comentario