ftell

Use ftell to find the file size

三世轮回 提交于 2020-07-21 07:20:07
问题 fseek(f, 0, SEEK_END); size = ftell(f); If ftell(f) tells us the current file position, the size here should be the offset from the end of the file to the beginning. Why is the size not ftell(f)+1? Should not ftell(f) only give us the position of the end of the file? 回答1: File positions are like the cursor in a text entry widget: they are in between the bytes of the file. This is maybe easiest to understand if I draw a picture: This is a hypothetical file. It contains four characters: a , b ,

Use ftell to find the file size

百般思念 提交于 2020-07-21 07:20:03
问题 fseek(f, 0, SEEK_END); size = ftell(f); If ftell(f) tells us the current file position, the size here should be the offset from the end of the file to the beginning. Why is the size not ftell(f)+1? Should not ftell(f) only give us the position of the end of the file? 回答1: File positions are like the cursor in a text entry widget: they are in between the bytes of the file. This is maybe easiest to understand if I draw a picture: This is a hypothetical file. It contains four characters: a , b ,

C语言fseek()函数和ftell()函数

时间秒杀一切 提交于 2020-02-06 04:25:35
C语言fseek()函数和ftell()函数 方便用的时候看, FILE * fp ; int ch ; long count = 0l , tell ; * * * 前移: * * * fseek ( fp , 0l , SEEK_END ) ; tell = ftell ( fp ) ; count = 1l ; //为了count成为-1 while ( count <= tell ) { fseel ( fp , - count , SEEK_END ) ; //从文件末尾向前移 ch = getc ( fp ) ; fprintf ( stdout , "%c" , ch ) ; count ++ ; } * * * 后移: * * * fseek ( fp , 0l , SEEK_END ) ; //定位到文件末尾,如果不写这个函数,下面的ftell()函数返回值为0; tell = ftell ( fp ) ; //从文件起始位置到文件末尾的字节数 printf ( "tell==%ld\n" , tell ) ; while ( count < tell ) { fseek ( fp , count , SEEK_SET ) ; //从文件的起始位置开始 ch = getc ( fp ) ; fprintf ( stdout , "%c" , ch ) ;

Why is fwrite writing more than I tell it to?

那年仲夏 提交于 2019-12-29 07:26:54
问题 FILE *out=fopen64("text.txt","w+"); unsigned int write; char *outbuf=new char[write]; //fill outbuf printf("%i\n",ftello64(out)); fwrite(outbuf,sizeof(char),write,out); printf("%i\n",write); printf("%i\n",ftello64(out)); output: 0 25755 25868 what is going on? write is set to 25755, and I tell fwrite to write that many bytes to a file, which is at the beginning, and then im at a position besides 25755? 回答1: If you are on a DOSish system (say, Windows) and the file is not opened in binary mode

Are there cases where fseek/ftell can give the wrong file size?

十年热恋 提交于 2019-12-22 08:19:48
问题 In C or C++, the following can be used to return a file size: const unsigned long long at_beg = (unsigned long long) ftell(filePtr); fseek(filePtr, 0, SEEK_END); const unsigned long long at_end = (unsigned long long) ftell(filePtr); const unsigned long long length_in_bytes = at_end - at_beg; fprintf(stdout, "file size: %llu\n", length_in_bytes); Are there development environments, compilers, or OSes which can return the wrong file size from this code, based on padding or other information

ftell error after the first call to fread

蹲街弑〆低调 提交于 2019-12-20 04:12:31
问题 So I have a very simple program that reads the 3 first bytes of a file: int main(void) { FILE *fd = NULL; int i; unsigned char test = 0; fd = fopen("test.bmp", "r"); printf("position: %ld\n", ftell(fd)); for (i=0; i<3; i++) { fread(&test, sizeof (unsigned char), 1, fd); printf("position: %ld char:%X\n", ftell(fd), test); } return (0); } When I try it with a text file it works fine: position: 0 position: 1 char: 61 position: 2 char: 62 position: 3 char: 63 but when I run it with a PNG for

ftell( stdin ) causes illegal seek error

对着背影说爱祢 提交于 2019-12-19 02:45:32
问题 The following code outputs "Illegal seek": #include <stdio.h> #include <errno.h> #include <string.h> int main() { errno = 0; getchar(); getchar(); getchar(); ftell( stdin ); printf( "%s\n", strerror(errno) ); } This occurs when I run "cat script | ./a.out" as well as when I just run "./a.out". The problem is with ftell, of course. My question is: why does this occur? I would think stdin can be seekable. fseek also causes the same error. If stdin is not seekable, is there some way I can do the

ftello/fseeko vs fgetpos/fsetpos

陌路散爱 提交于 2019-12-18 02:49:49
问题 What is the difference between ftello/fseeko and fgetpos/fsetpos? Both seem to be file pointer getting/setting functions that use opaque offset types to sometimes allow 64 bit offsets. Are they supported on different platforms or by different standards? Is one more flexible in the type of the offset it uses? And, by the way, I am aware of fgetpos/fsetpos and ftell/fseek, but this is not a duplicate. That question asks about ftell/fseek, and the answer is not applicable to ftello/fseeko. 回答1:

ftell returning incorrect value

泄露秘密 提交于 2019-12-11 06:41:24
问题 I am having a problem where ftell is returning an incorrect value. My code, when run in netbeans on linux reports correctly, but the exact same code, running in netbeans on windows (using mingw) reports incorrectly. the file pointer is to a file opened in BINARY_READ. in my linux netbeans, after running my subroutine, the ftell reports 35. in my windows netbeans, the after calling the same subroutine, the ftell is 3621. I traced through my subroutine and the following statement appears to

Get position for file descriptor in Python

笑着哭i 提交于 2019-12-07 08:32:25
问题 Say, I have a raw numeric file descriptor and I need to get the current position in file based on it. import os, psutil # some code that works with file lp = lib.open('/path/to/file') p = psutil.Process(os.getpid()) fd = p.get_open_files()[0].fd # int while True: buf = lp.read() if buf is None: break device.write(buf) print tell(fd) # how to find where we are now in the file? In the following code lib is a compiled library, that doesn't give access to the file object. In the loop I use the