c语言ftell函数
C中的ftell()函数 (ftell() function in C)
Prototype:
原型:
long int ftell(FILE *filename);
Parameters:
参数:
FILE *filename
Return type: long int
返回类型: long int
Use of function:
使用功能:
The ftell() function returns the current value of the file indicator pointer for the specified stream. In case of binary stream it returns the position in terms of bytes from the beginning. The prototype of the function ftell() is:
ftell()函数返回指定流的文件指示器指针的当前值。 如果是二进制流,它将从头开始以字节为单位返回位置。 函数ftell()的原型为:
long int ftell(FILE *filename);
When an error is occurred, it returns -1.
发生错误时,它返回-1。
C中的ftell()示例 (ftell() example in C)
#include <stdio.h>
#include <stdlib.h>
int main(){
//Initialize the file pointer
FILE *f;
//Take a array of characters
char ch[100];
//Create the file for write operation
f=fopen("includehelp.txt","w");
printf("Enter five strings\n");
for(int i=0;i<4;i++){
//take the strings from the users
scanf("%[^\n]",&ch);
//write back to the file
fputs(ch,f);
//every time take a new line for the new entry string
//except for last entry.Otherwise print the last line twice
fputs("\n",f);
//clear the stdin stream buffer
fflush(stdin);
}
//take the strings from the users
scanf("%[^\n]",&ch);
fputs(ch,f);
//close the file after write operation is over
fclose(f);
//open a file
f=fopen("includehelp.txt","r");
printf("\n...............print the strings..............\n");
while(!feof(f)){
//takes the first 100 character in the character array
fgets(ch,100,f);
//and print the strings
printf("%s",ch);
}
rewind(f);
printf("\n...............print the position after print a strings ..............\n");
fgets(ch,100,f);
printf("%s",ch);
//print the current location
printf("the file indicator position is - %d\n",ftell(f));
//close the file
fclose(f);
return 0;
}
Output
输出量
翻译自: https://www.includehelp.com/c-programs/ftell-function-in-c-language-with-example.aspx
c语言ftell函数