fseek

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

↘锁芯ラ 提交于 2019-11-27 07:37:57
问题 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.

fseek does not work when file is opened in “a” (append) mode

寵の児 提交于 2019-11-27 05:45:55
问题 FILE* f = fopen("rajat", "w"); fputs("sometext", f); fseek(f, 6, SEEK_SET); fputs("is a", f); fclose(f); Successfully returns: "someteis a" But FILE* f = fopen("rajat", "a"); fputs("sometext", f); fseek(f, 6, SEEK_SET); fputs("is a", f); fclose(f); Does not work. Returns "sometextis a" Any ideas why? What is the solution to this, so that the second code outputs exactly like the first? 回答1: When you open in append mode, the file pointer is returned to the end of file before every write. You

fseek vs rewind?

故事扮演 提交于 2019-11-27 02:10:21
问题 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? 回答1: 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

Read a file backwards line by line using fseek

徘徊边缘 提交于 2019-11-26 19:04:16
How do I read a file backwards line by line using fseek? code can be helpful. must be cross platform and pure php. many thanks in advance regards Jera The question is asking using fseek, so can only assume that performance is an issue and file() is not the solution. Here is a simple approach using fseek: My file.txt #file.txt Line 1 Line 2 Line 3 Line 4 Line 5 And the code: <?php $fp = fopen('file.txt', 'r'); $pos = -2; // Skip final new line character (Set to -1 if not present) $lines = array(); $currentLine = ''; while (-1 !== fseek($fp, $pos, SEEK_END)) { $char = fgetc($fp); if (PHP_EOL ==

Why is fseek or fflush always required between reading and writing in the update modes?

我是研究僧i 提交于 2019-11-26 14:41:32
Q: I'm trying to update a file in place, by using fopen mode "r+" , reading a certain string, and writing back a modified string, but it's not working. A: Be sure to call fseek before you write, both to seek back to the beginning of the string you're trying to overwrite, and because an fseek or fflush is always required between reading and writing in the read/write "+" modes. My question is why fseek or fflush is always required between reading and writing in the read/write "+" modes? Section 5.2 of Andrew Koenig's C Traps and Pitfalls (1989) mentioned that it is because of a backward

Read a file backwards line by line using fseek

本秂侑毒 提交于 2019-11-26 06:47:00
问题 How do I read a file backwards line by line using fseek? code can be helpful. must be cross platform and pure php. many thanks in advance regards Jera 回答1: The question is asking using fseek, so can only assume that performance is an issue and file() is not the solution. Here is a simple approach using fseek: My file.txt #file.txt Line 1 Line 2 Line 3 Line 4 Line 5 And the code: <?php $fp = fopen('file.txt', 'r'); $pos = -2; // Skip final new line character (Set to -1 if not present) $lines =