Do files get closed during an exception exit?

A fairly straightforward question. Two answers. One saying, “Yes.” The other saying, “No!” Both with significant upvotes. Who to believe? Let me attempt to clarify. Both answers have some truth to them, and it depends on what you mean by a file being closed. First, consider what is meant by closing a file from the … Read more

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

Reading a .pdb file

If you mean PDB as in a “program database” that the debugger uses: PDB files contain data about a file such as an EXE or DLL that is used to aid in debugging. There are public interfaces that allow you to extract data from the file. See examples here: (Moved from If … Read more

Using Directory.GetFiles with a regex in C#?

Directory.GetFiles doesn’t support RegEx by default, what you can do is to filter by RegEx on your file list. Take a look at this listing: Regex reg = new Regex(@”^^(?!p_|t_).*”); var files = Directory.GetFiles(yourPath, “*.png; *.jpg; *.gif”) .Where(path => reg.IsMatch(path)) .ToList();

Getting file extension in C

const char *get_filename_ext(const char *filename) { const char *dot = strrchr(filename, ‘.’); if(!dot || dot == filename) return “”; return dot + 1; } printf(“%s\n”, get_filename_ext(“test.tiff”)); printf(“%s\n”, get_filename_ext(“test.blah.tiff”)); printf(“%s\n”, get_filename_ext(“test.”)); printf(“%s\n”, get_filename_ext(“test”)); printf(“%s\n”, get_filename_ext(“…”));

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”) info = tarfile.TarInfo(name=”foo”) info.size=len(string.buf) tar.addfile(tarinfo=info, fileobj=string) tar.close()

iOS: How do you find the creation date of a file?

This code actually returns the good creation date to me: NSFileManager* fm = [NSFileManager defaultManager]; NSDictionary* attrs = [fm attributesOfItemAtPath:path error:nil]; if (attrs != nil) { NSDate *date = (NSDate*)[attrs objectForKey: NSFileCreationDate]; NSLog(@”Date Created: %@”, [date description]); } else { NSLog(@”Not found”); } Are you creating the file inside the App? Maybe that’s where the … Read more

My async call is returning before list is populated in forEach loop

This code Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename) async { final file = await File(filename); String contents = await file.readAsString(); User user = User.fromJson(json.decode(contents)); String name = user.NameLast + “, ” + user.NameFirst; print(name); l.add(name); } return l; } returns the list l … Read more