Is there a way to use fopen_s() with GCC or at least create a #define about it?

Microsoft’s *_s functions are unportable, I usually use equivalent C89/C99 functions and disable deprecation warnings (#define _CRT_SECURE_NO_DEPRECATE). If you insist, you can use an adaptor function (not necessarily a macro!) that delegates fopen() on platforms that don’t have fopen_s(), but you must be careful to map values of errno_t return code from errno. errno_t fopen_s(FILE … Read more

How do I open a file from line X to line Y in PHP?

Yes, you can do that easily with SplFileObject::seek $file = new SplFileObject(‘filename.txt’); $file->seek(1000); for($i = 0; !$file->eof() && $i < 1000; $i++) { echo $file->current(); $file->next(); } This is a method from the SeekableIterator interface and not to be confused with fseek. And because SplFileObject is iterable you can do it even easier with a … Read more

Overwrite Line in File with PHP

If the file is really big (log files or something like this) and you are willing to sacrifice speed for memory consumption you could open two files and essentially do the trick Jeremy Ruten proposed by using files instead of system memory. $source=”in.txt”; $target=”out.txt”; // copy operation $sh=fopen($source, ‘r’); $th=fopen($target, ‘w’); while (!feof($sh)) { $line=fgets($sh); … Read more