Function ftell () in C

The function long int ftell (FILE * stream) in standard C Library returns the current file location of the given Stream.

Declare ftell () function in C

Below is the declaration for ftell () function in C:

 long int ftell ( FILE * stream ) 

Parameters

stream - This is the pointer to a FILE object that identifies the Stream.

Returns the value

This function returns the current value of position indicator. If an error occurs, -1 is returned, and the global variable errno is set to a positive value.

For example

The following program C illustrates the usage of the ftell () function in C:

 #include int main () { FILE * fp ; int len ; fp = fopen ( "baitapc.txt" , "r" ); if ( fp == NULL ) { perror ( "Xay ra loi khi mo file!!!" ); return (- 1 ); } fseek ( fp , 0 , SEEK_END ); len = ftell ( fp ); fclose ( fp ); printf ( "Tong kich co cua baitapc.txt = %d bytesn" , len ); return ( 0 ); } 

Suppose we have the bait.txt file with the following content:

 Hoc C co ban va nang cao tai QTM !!! 

Compiling and executing the above program will produce the following result if the file has the above content, otherwise it will give different results depending on the content of the file.

Function ftell () in C Picture 1

According to Tutorialspoint

Previous post: Function fsetpos () in C

Next lesson: Function fwrite () in C

4 ★ | 5 Vote

May be interested

  • Function fwrite () in CPhoto of Function fwrite () in C
    function size_t fwrite (const void * ptr, size_t size, size_t nmemb, file * stream) in standard c library writes data from the array pointed by ptr to the given stream.
  • Union in CPhoto of Union in C
    a union is a special data in c language that allows you to reserve different data types in the same memory. you can define union with a lot of parameters, but only one component contains values ​​at a time. union provides an effective way to use a device for multiple purposes.
  • Bit field in CPhoto of Bit field in C
    suppose your c program includes a number of true / false variables grouped in a structure called a page to check if the manufactured goods have sufficient width and height allowed.
  • Keyword typedef in CPhoto of Keyword typedef in C
    the c program language provides a typedef keyword, which you can use to provide the type for a new name. here is an example to define a byte entry for 1-byte numbers (like unsigned char).
  • Input & Output in CPhoto of Input & Output in C
    when we talk about input, we are talking about input data for the program. it can be provided from the command line or from a certain file. program c language provides a set of functions available to read the entered data and provide it for the required programs.
  • Read - Write File in CPhoto of Read - Write File in C
    the previous chapter explained the standard input and output devices handled by the c language. in this chapter we will see how programmers create, open and close text files or binary files with data. storage.