C-Language file operations 11--file functions re-speak fseek () and Ftell ()

Source: Internet
Author: User

//file function re-speak//fseek (), Ftell (),#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>voidMain () {//Defining file Paths    Char*path ="E:\\look\\b1.txt";//Support window only    Char*path1 ="E:/look/b1.txt";//support Windows and Linux//Defining file PointersFILE *PF =NULL; //open a file as read-writePF = fopen (path1,"r+"); //determine if the file is open successfully    if(pf==NULL) {printf ("File open failed! File path is%s\n", path1); }    //int fseek (FILE *stream, long offset, int origin); //the first parameter stream is a file pointer//The second parameter offset is offset, positive number indicates positive offset, negative is 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: File Start//seek_cur: Current Location//seek_end: End of File//among them Seek_set, Seek_cur and Seek_end are 0, 1 and 2 in turn. //In short://fseek (FP, 100L, 0); Move the stream pointer to 100 bytes from the beginning of the file;//fseek (FP, 100L, 1); Move the stream pointer to 100 bytes from the current position of the file;//fseek (FP, -100l, 2); Returns the stream pointer to 100 bytes from the end of the file.     Charbuf[ -] = {0}; Fgets (BUF, -, PF); printf ("Output result%s\n", BUF); //move the file pointer to the end of the fileFseek (PF,0L, Seek_end); //calculates the file pointer offset (essentially the size of the file)    intnum =Ftell (PF); //function Name: Ftell//function prototype: Long Ftell (FILE *stream); //function function//The number of offset bytes used to get the current position of the file position pointer relative to the top of the file. (using the Fseek function and then calling the function Ftell () makes it very easy to determine the current position of the file.) )//constraint conditions//because Ftell returns a long type, there is an error in manipulating files larger than 2.1G, depending on the range of long values-231~231-1 (-2147483648~2147483647). printf"file size%d bytes \ n", num); //Close File    if(pf!=NULL)    {fclose (PF); } System ("Pause");}

C-Language file operations 11--file functions re-speak fseek () and Ftell ()

Related Article

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.