ftell

End of FILE* pointer is not equal to size of written data

扶醉桌前 提交于 2019-12-06 22:30:25
问题 Very simply put, I have the following code snippet: FILE* test = fopen("C:\\core.u", "w"); printf("Filepointer at: %d\n", ftell(test)); fwrite(data, size, 1, test); printf("Written: %d bytes.\n", size); fseek(test, 0, SEEK_END); printf("Filepointer is now at %d.\n", ftell(test)); fclose(test); and it outputs: Filepointer at: 0 Written: 73105 bytes. Filepointer is now at 74160. Why is that? Why does the number of bytes written not match the file pointer? 回答1: Since you're opening the file in

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

家住魔仙堡 提交于 2019-12-05 15:07:01
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 that is situation-specific? Were there changes in the C or C++ specification around 1999, which would have

Get position for file descriptor in Python

余生长醉 提交于 2019-12-05 12:53:35
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 embedded method read which returns the processed data. The data and it's length doesn't relate to the

C++->文件流的输入输出

自闭症网瘾萝莉.ら 提交于 2019-12-03 23:17:00
C++->文件流的输入输出 1.书本里以“简单事务处理”为例子,解析二进制输入输出文件流的read和write函数的使用,以及输入输出文件流 过程中指针的捕获、定位,文件流位置的判断,二进制文件转换为文本文件。 1.1.二进制文件流的read函数的原型:read(char *buffer,streamsize size); 1.2.二进制文件流的write函数的原型:write(char *buffer,streamsize size); 注释:这里buffer是一块内存的地址,用来存储或读出数据。参数size是一个整数值,表示用从缓存(buffer)中读出或写入的字 符数。 1.3.移动,获取指针位置函数 1.3.1.移动读、写指针位置 istream&/input.seekg(20,ios::beg); //以流开始位置为基准,后移20个字 istream&/input.seekg(-10,ios::cur); //以指针当前位置为基准,前移10个字 istream&/input.seekg(-10,ios::end); //以指针结尾位置为基准,前移10个字 ostream&/output.seekp()移动(写读)指针位置与seekg类似, ostream&/output.seekp(120); //output流的写指针移到第120个(字节)处 input.tellg(

Overwrite to a specific line in c

别来无恙 提交于 2019-12-03 21:56:51
问题 I have a file of about 2000 lines of text that i generate in my program, every line has the information of an employee and it's outputed like this 1 1 Isaac Fonseca 58 c 1600 1310.40 6 1 0.22 2164.80 1 2 1 Manuel Gutierrez 22 d 1700 1523.37 4 1 0.13 897.26 1 3 1 Daniel Bernal 34 c 1600 1195.84 2 1 0.26 836.16 1 4 1 Miguel Gonzalez 43 e 1800 1195.84 0 1 0.15 0.00 1 But i whenever i edit an employee information i have to update the file, what i'm doing it's i search for the line and try to

ftell error after the first call to fread

对着背影说爱祢 提交于 2019-12-02 07:14:06
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 example I get: position: 0 position: 147 char:89 position: 148 char:50 position: 149 char:4E Note that the

ftell on a file descriptor?

孤街浪徒 提交于 2019-11-29 16:39:56
问题 Is there a way to do what ftell() does (return the current position in the file) on a raw file descriptor instead of a FILE*? I think there ought to be, since you can seek on a raw file descriptor using lseek(). I know I could use fdopen() to create a FILE* corresponding to the file descriptor, but I'd rather not do that. 回答1: Just use: position = lseek(fd, 0, SEEK_CUR); 来源: https://stackoverflow.com/questions/3399236/ftell-on-a-file-descriptor

Why is fwrite writing more than I tell it to?

扶醉桌前 提交于 2019-11-29 09:23:26
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? If you are on a DOSish system (say, Windows) and the file is not opened in binary mode, line-endings will be converted automatically and each "line" will add one byte. So, specify "wb" as the

ftello/fseeko vs fgetpos/fsetpos

坚强是说给别人听的谎言 提交于 2019-11-28 23:41:13
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. See Portable Positioning for detailed information on the difference. An excerpt: On some systems where text

ftell at a position past 2GB

送分小仙女□ 提交于 2019-11-27 15:34:25
On a 32-bit system, what does ftell return if the current position indicator of a file opened in binary mode is past the 2GB point? In the C99 standard, is this undefined behavior since ftell must return a long int (maximum value being 2**31-1 )? Ahmed Masud on long int long int is supposed to be AT LEAST 32-bits, but C99 standard does NOT limit it to 32-bit. C99 standard does provide convenience types like int16_t & int32_t etc that map to correct bit sizes for a target platform. on ftell/fseek ftell() and fseek() are limited to 32 bits (including sign bit) on the vast majority of 32-bit