Detailed explanation of the use of fseek function and Ftell function in C language _c language

Source: Internet
Author: User
Tags fread

fseek function:

int fseek (FILE * _file, long _offset, int _origin); 

function to set the location of the file pointer stream. If the execution succeeds, the stream points to the position of Fromwhere as the offset (pointer offset) byte, and the function returns 0. If execution fails, the stream point is not changed, and the function returns a value other than 0.
Goes beyond the end of the file or returns 0. Go back to the first position, or return 0, use caution.
The first parameter stream is a file pointer.
The second parameter offset is an offset, a positive number indicates a positive offset, and a negative amount represents a negative offset.
The third parameter, origin, sets the offset from where the file begins, possibly with a value of: Seek_cur, Seek_end, or Seek_set.
Seek_set: Beginning of File
Seek_cur: Current Position
Seek_end: End of File
eg

#include <stdio.h> #define N 5 typedef struct student{long sno;
 Char name[10];
float score[3];
 
}stu;
 void Fun (Char*filename,stu n) {file*fp;
 Fp=fopen (filename, "rb+");
 Fseek (Fp,-1l*sizeof (STU), seek_end);
 Fwrite (&n,sizeof (STU), 1,FP);
Fclose (FP); int main ()/* Modify overwrites last student data * * {STU t[n]={{10001, "Machao", 91,92,77}, {10002, "Caokai", 75,60,88}, {10003, "LiSi", 85,
  
 70,78}, {10004, "Fangfang", 90,82,87}, {10005, "Zhangsan", 95,80,88}};
 
 STU n={10006, "Zhaosi", 55,70,68},ss[n]; int i,j;
  
 FILE*FP;
  
 Fp=fopen ("Student.dat", "WB");
  
 Fwrite (T,sizeof (STU), N,FP);
  
 Fclose (FP);
  
 Fp=fopen ("Student.dat", "RB");
  
 Fread (Ss,sizeof (STU), N,FP);
  
 Fclose (FP);
  
 printf ("\nthe original data:\n\n");
  for (j=0;j<n;j++) {printf ("\nno:%ldname:%-8sscores:", ss[j].sno,ss[j].name);
  for (i=0;i<3;i++) printf ("%6.2f", Ss[j].score[i]);
 printf ("\ n");
 } Fun ("Student.dat", N);
 
 printf ("\nthe data after modifing:\n\n");
 Fp=fopen ("Student.dat", "RB"); Fread (SS,sizeof (STU), N,FP);
 
 Fclose (FP);
  for (j=0;j<n;j++) {printf ("\nno:%ldname:%-8sscores:", ss[j].sno,ss[j].name);
  for (i=0;i<3;i++) printf ("%6.2f", Ss[j].score[i]);
 printf ("\ n");
return 0;
 }

Ftell function
the Ftell function is used to get the number of offset bytes at the current position of the file position pointer relative to the top of the file. When accessing files randomly, the program is not easy to determine the current location of the file because of the frequent movement of the file location.

Long len = Ftell (FP) 

EG1:

#include <stdio.h>
int main (void)
{
 FILE *stream;
 stream = fopen ("MYFILE.") TXT "," w+ ");
 fprintf (Stream, "This is a Test");
 printf ("The file pointer is at byte \
%ld\n", Ftell (Stream));
 Fclose (stream);
 return (0);
}

EG2:
Ftell is typically used to read the length of a file, the following is an example that reads the contents of a text file:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 FILE *fp;
 int Flen;
 char *p;
/* Read-only open file */
 if (fp = fopen ("1.txt", "r") = = NULL)
 {
 printf ("\nfile open error\n");
 Exit (0);
 }
 Fseek (FP, 0L, seek_end);       /* Navigate to the end of the file
 /Flen = Ftell (FP);         /* Get File Size * *
 p = (char *) malloc (Flen + 1);/* Dynamically allocate memory space based on file size *
 /if (p = = NULL)
 {
 fclose (FP); 
   return (0);
 }
 Fseek (FP, 0L, seek_set);       /* Navigate to the beginning of the file
 /fread (P, Flen, 1, FP);        * * Read all the contents of the document
 /P[flen] = ' the ';             /* string End flag
 /printf ("%s", p);
 Fclose (FP);
 Free (p);
 return (0);
}


Program Improvement

#include <stdio.h>
Main ()
{
 FILE *myf;
 Long F1; /* Here set F1 to long to read longer files
 /Myf = fopen ("1.txt", "RB");
 Fseek (MYF, 0, seek_end);
 F1 = Ftell (MYF);
 Fclose (MYF);
 printf ("% d \ n", F1);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.