freopen() equivalent for c++ streams

freopen also works with cin and cout. No need to search for something new. freopen(“input.txt”, “r”, stdin); // redirects standard input freopen(“output.txt”, “w”, stdout); // redirects standard output int x; cin >> x; // reads from input.txt cout << x << endl; // writes to output.txt Edit: From C++ standard 27.3.1: The object cin controls … Read more

How to read all files in a folder using C

You can use this sample code and modify it if you need: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <dirent.h> #include <unistd.h> #include <errno.h> /* This is just a sample code, modify it to meet your need */ int main(int argc, char **argv) { DIR* FD; struct dirent* in_file; FILE *common_file; FILE *entry_file; … Read more

File.ReadLines without locking it?

No… If you look with Reflector you’ll see that in the end File.ReadLines opens a FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 0x1000, FileOptions.SequentialScan); So Read-only share. (it technically opens a StreamReader with the FileStream as described above) I’ll add that it seems to be child’s play to make a static method to do it: public static IEnumerable<string> … Read more

Creating temporary folders

Update: Added File.Exists check per comment (2012-Jun-19) Here’s what I’ve used in VB.NET. Essentially the same as presented, except I usually didn’t want to create the folder immediately. The advantage to use GetRandomFilename is that it doesn’t create a file, so you don’t have to clean up if your using the name for something other … Read more

Reading a file character by character in C

There are a number of things wrong with your code: char *readFile(char *fileName) { FILE *file; char *code = malloc(1000 * sizeof(char)); file = fopen(fileName, “r”); do { *code++ = (char)fgetc(file); } while(*code != EOF); return code; } What if the file is greater than 1,000 bytes? You are increasing code each time you read … Read more

java file input with rewind()/reset() capability

I think the answers referencing a FileChannel are on the mark . Here’s a sample implementation of an input stream that encapsulates this functionality. It uses delegation, so it’s not a true FileInputStream, but it is an InputStream, which is usually sufficient. One could similarly extend FileInputStream if that’s a requirement. Not tested, use at … Read more

Write to a file from multiple threads asynchronously c#

For those who prefer code, I am using following to do remote logging from web apps… public static class LoggingExtensions { static ReaderWriterLock locker = new ReaderWriterLock(); public static void WriteDebug(this string text) { try { locker.AcquireWriterLock(int.MaxValue); //You might wanna change timeout value System.IO.File.AppendAllLines(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Replace(“file:\\”, “”), “debug.txt”), new[] { text }); } finally { locker.ReleaseWriterLock(); } … Read more