Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

directory

  • I. Introduction to ftell function
  • Ftell function practice
  • Classical use of ftell function
  • Four. Guess you like it

C/C++ learning Directory >> C language basics

I. Introduction to ftell function

The C function ftell is used to get the number of bytes offset from the current position of the file pointer to the start of the file. The ftell function is declared as follows:

#include <stdio.h> #include <stdlib.h> /* * Description: Returns the current file location of the given stream * * Parameter: * [in] stream: file pointer handle * * If successful, the function returns the current value of the location identifier; If an error occurs, -1L is returned; */ long int ftell(FILE *stream);Copy the code

Ftell function practice

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language File read and write ftell function // @motto: a thousand miles without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include <stdio.h> #include <stdlib.h>//fseek calls int main() {long position; char list[100]; /* rb+ read/write Opens a binary file, allowing data to be read. */ FILE * fp = fopen("a.txt", "rb+"); if (fp == NULL) { printf("file error\n"); exit(1); } fread(list,sizeof(char),100,stream); //get position after read position=ftell(stream); printf("Position after trying to read 100 bytes:%ld\n",position); fclose(stream); stream=NULL; return 0; }Copy the code

Classical use of ftell function

If you don’t know the size of the file, you can use the ftell function to get the size of the file, and then use the fread function to get the size of the buffer.

/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / / / @ Author: programming ape said // @blog (personal Blog address): www.codersrc.com // @file :C language tutorial - C language File read and write ftell function // @motto: a thousand miles without a small step, a river without a small stream, The wonderful program life needs to accumulate unremittingly! /******************************************************************************************/ #include <stdio.h> #include  <stdlib.h> int main() { FILE *fp; int flen; char *p; To open the file read-only way / * * / if ((fp = fopen (" 1. TXT ", "r")) = = NULL) {printf (" \ nfile open error \ n "); exit(0); } fseek(fp,0L,SEEK_END); Flen =ftell(fp); flen=ftell(fp); */ p=(char *)malloc(flen+1); If (p==NULL) {fclose(fp); return 0; } fseek(fp,0L,SEEK_SET); /* locate to the beginning of file */ fread(p,flen,1,fp); P [flen]='\0'; /* End of string */ printf("%s",p); fclose(fp); free(p); return 0; }Copy the code

Four. Guess you like it

  1. C array subscript out of bounds and memory overflow difference
  2. C language uses Pointers to iterate over groups of numbers
  3. C language pointer and array difference
  4. C language pointer array and array pointer difference
  5. C language field pointer
  6. C function value passing and address passing
  7. C language function variable parameter
  8. C function pointer
  9. C language pointer function
  10. C language callback function callback
  11. Pragma once
  12. C language #include <> is different from #include
  13. C const decorates function arguments
  14. C const is different from define
  15. C # operator
  16. C language ## operator
  17. C language __VA_ARGS__
  18. C # # __VA_ARGS__
  19. C function variable length argument ##__VA_ARGS__ classic case
  20. C va_start/va_end/va_arg User-defined printf functions
  21. C main function
  22. Int argc, char *argv[]
  23. C language local variables
  24. C language global variables
  25. C language global variable and local variable difference
  26. The static C
  27. C language extern

C language file read and write ftell function

This article is published by the blog – Ape Say Programming Ape Say programming!