Read last line from file

This should work: $line=””; $f = fopen(‘data.txt’, ‘r’); $cursor = -1; fseek($f, $cursor, SEEK_END); $char = fgetc($f); /** * Trim trailing newline chars of the file */ while ($char === “\n” || $char === “\r”) { fseek($f, $cursor–, SEEK_END); $char = fgetc($f); } /** * Read until the start of file or first newline char … Read more

python write string directly to tarfile

I would say it’s possible, by playing with TarInfo e TarFile.addfile passing a StringIO as a fileobject. Very rough, but works import tarfile import StringIO tar = tarfile.TarFile(“test.tar”,”w”) string = StringIO.StringIO() string.write(“hello”) string.seek(0) info = tarfile.TarInfo(name=”foo”) info.size=len(string.buf) tar.addfile(tarinfo=info, fileobj=string) tar.close()

How to remove tmp directory files of an ios app?

Yes. This method works well: + (void)clearTmpDirectory { NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL]; for (NSString *file in tmpDirectory) { [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@”%@%@”, NSTemporaryDirectory(), file] error:NULL]; } }

Can .NET load and parse a properties file equivalent to Java Properties class?

No there is no built-in support for this. You have to make your own “INIFileReader”. Maybe something like this? var data = new Dictionary<string, string>(); foreach (var row in File.ReadAllLines(PATH_TO_FILE)) data.Add(row.Split(‘=’)[0], string.Join(“=”,row.Split(‘=’).Skip(1).ToArray())); Console.WriteLine(data[“ServerName”]); Edit: Updated to reflect Paul’s comment.

Append to a file in Go

This answers works in Go1: f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) if err != nil { panic(err) } defer f.Close() if _, err = f.WriteString(text); err != nil { panic(err) }

Read binary file as string in Ruby

First, you should open the file as a binary file. Then you can read the entire file in, in one command. file = File.open(“path-to-file.tar.gz”, “rb”) contents = file.read That will get you the entire file in a string. After that, you probably want to file.close. If you don’t do that, file won’t be closed until … Read more

tech