fseek

fseek on a position beyond EOF does not trigger EOF using feof, how come?

怎甘沉沦 提交于 2019-11-29 04:01:31
I'm reading data from a file to memory that is opened with: FILE *f = fopen(path, "rb"); Before I start copying bytes from the file I seek to a start position using: /** * Goes to the given position of the given file. * * - Returns 0 on success * - Returns -1 on EOF * - Returns -2 if an error occured, see errno for error code * - Returns -3 if none of the above applies. This should never happen! */ static int8_t goto_pos(FILE *f, uint64_t pos) { int err = fseek(f, pos, SEEK_SET); if (err != 0) { if (feof(f) != 0) return -1; if (ferror(f) != 0) return -2; return -3; } return 0; } The problem is

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

php ip转换省市县

送分小仙女□ 提交于 2019-11-28 15:23:29
http://www.cz88.net/ip/ http://www.ttlsa.com/php/php_cunzhen-ipdata/ #  wget h http://6.scdx3.crsky.com/201307/qqwry0715.zip #  unzip qqwry0715.zip #  mv ip/qqwry.dat /data/site/test.ttlsa.com/qqwry.dat p class cls_ipAddress{ private $fp; private $firstip; private $lastip; private $totalip; public function __construct($filename="qqwry.dat"){ $this->fp=0; if(($this->fp=@fopen($filename,"rb"))!==false){ $this->firstip=$this->getlong(); $this->lastip=$this->getlong(); $this->totalip=($this->lastip-$this->firstip)/7; register_shutdown_function(array(&$this,"__destruct")); } } public function _

Using fseek and ftell to determine the size of a file has a vulnerability?

泄露秘密 提交于 2019-11-28 13:22:43
I've read posts that show how to use fseek and ftell to determine the size of a file. FILE *fp; long file_size; char *buffer; fp = fopen("foo.bin", "r"); if (NULL == fp) { /* Handle Error */ } if (fseek(fp, 0 , SEEK_END) != 0) { /* Handle Error */ } file_size = ftell(fp); buffer = (char*)malloc(file_size); if (NULL == buffer){ /* handle error */ } I was about to use this technique but then I ran into this link that describes a potential vulnerability. The link recommends using fstat instead. Can anyone comment on this? The link is one of the many nonsensical pieces of C coding advice from CERT

fseek vs rewind?

こ雲淡風輕ζ 提交于 2019-11-28 08:21:48
I have noticed two methods to return to the beginning of a file FILE *fp = fopen("test.bin", "r") fseek(fp, 0, SEEK_END); rewind(fp); and FILE *fp = fopen("test.bin", "r") fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_SET); What would be difference if any between these methods? They are basically two different ways to accomplish the same thing: set the pointer to the beginning of the file. The only difference is that rewind also clears the error indicator. If given the choice, you should use fseek . This is because rewind doesn't return an integer indicating whether the operation has succeeded. If

Complexity of f.seek() in Python

て烟熏妆下的殇ゞ 提交于 2019-11-28 07:39:13
问题 Does f.seek(500000,0) go through all the first 499999 characters of the file before getting to the 500000th? In other words, is f.seek(n,0) of order O(n) or O(1)? 回答1: You need to be a bit more specific on what type of object f is. If f is a normal io module object for a file stored on disk, you have to determine if you are dealing with: The raw binary file object A buffer object, wrapping the raw binary file A TextIO object, wrapping the buffer An in-memory BytesIO or TextIO object The first

Understanding undefined behavior for a binary stream using fseek(file, 0, SEEK_END) with a file

谁都会走 提交于 2019-11-28 07:09:40
问题 The C spec has an interesting footnote (#268 C11dr §7.21.3 9) "Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END) , has undefined behavior for a binary stream (because of possible trailing null characters) or for any stream with state-dependent encoding that does not assuredly end in the initial shift state." Does this ever apply to binary streams reading a file? (as from a physical device) IMO, a binary file on a disk is just a sea of bytes. It seems to me

Does fseek() move the file pointer to the beginning of the file if it was opened in “a+b” mode?

房东的猫 提交于 2019-11-28 03:13:40
问题 I wish to open a file using the "a+b" mode, i.e. if it does not exist it is created automatically, but if it does I don't want to overwrite it. I want to be able to read and write to the file. The file is binary, and I want to save records of a specific struct in it. So I want to do fseek() to the record I want and then save the record using fwrite() . The code looks as follows ( MyRecord is a typedef to a struct , while FILENAME is a #define to the file's name): int saveRecord(MyRecord

Append to the end of a file in C

江枫思渺然 提交于 2019-11-27 20:23:37
I'm trying to append the contents of a file myfile.txt to the end of a second file myfile2.txt in c. I can copy the contents, but I can't find a way to append. Here's my code: FILE *pFile; FILE *pFile2; char buffer[256]; pFile=fopen("myfile.txt", "r"); pFile2=fopen("myfile2.txt", r+); if(pFile==NULL) { perror("Error opening file."); } else { while(!feof(pFile)) { if(fgets(buffer, 100, pFile) != NULL) { fseek(pFile2, -100, SEEK_END); fprintf(pFile2, buffer); } } fclose(pFile); fclose(pFile2); I don't think I'm using fseek correctly, but what I'm trying to do is call fseek to put the pointer at

Using fseek to backtrack

北城余情 提交于 2019-11-27 07:54:11
问题 Is using fseek to backtrack character fscanf operations reliable? Like for example if I have just fscanf -ed 10 characters but I would like to backtrack the 10 chars can I just fseek(infile, -10, SEEK_CUR) ? For most situations it works but I seem to have problems with the character ^M . Apparently fseek registers it as a char but fscanf doesn't register it, thus in my previous example a 10 char block containing a ^M would require fseek(infile, -11, SEEK_CUR) instead. fseek(infile, -10, SEEK