C++/Win32: How to wait for a pending delete to complete

There are other processes in Windows that want a piece of that file. The search indexer is an obvious candidate. Or a virus scanner. They’ll open the file for full sharing, including FILE_SHARE_DELETE, so that other processes aren’t heavily affected by them opening the file. That usually works out well, unless you create/write/delete at a … 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

File Copy with Progress Bar

You need something like this: public delegate void ProgressChangeDelegate(double Percentage, ref bool Cancel); public delegate void Completedelegate(); class CustomFileCopier { public CustomFileCopier(string Source, string Dest) { this.SourceFilePath = Source; this.DestFilePath = Dest; OnProgressChanged += delegate { }; OnComplete += delegate { }; } public void Copy() { byte[] buffer = new byte[1024 * 1024]; // … Read more

Writing Structs to a file in c [closed]

Is it possible to write an entire struct to a file Your question is actually writing struct instances into file. You can use fwrite function to achieve this. You need to pass the reference in first argument. sizeof each object in the second argument Number of such objects to write in 3rd argument. File pointer … Read more

Continuously read file with C#

More natural approach of using FileSystemWatcher: var wh = new AutoResetEvent(false); var fsw = new FileSystemWatcher(“.”); fsw.Filter = “file-to-read”; fsw.EnableRaisingEvents = true; fsw.Changed += (s,e) => wh.Set(); var fs = new FileStream(“file-to-read”, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); using (var sr = new StreamReader(fs)) { var s = “”; while (true) { s = sr.ReadLine(); if (s != … Read more

How do I create an incrementing filename

I would iterate through sample[int].xml for example and grab the next available name that is not used by a file or directory. import os i = 0 while os.path.exists(“sample%s.xml” % i): i += 1 fh = open(“sample%s.xml” % i, “w”) …. That should give you sample0.xml initially, then sample1.xml, etc. Note that the relative file … Read more

Matrix from Python to MATLAB

If you use numpy/scipy, you can use the scipy.io.savemat function: import numpy, scipy.io arr = numpy.arange(9) # 1d array of 9 numbers arr = arr.reshape((3, 3)) # 2d array of 3×3 scipy.io.savemat(‘c:/tmp/arrdata.mat’, mdict={‘arr’: arr}) Now, you can load this data into MATLAB using File -> Load Data. Select the file and the arr variable (a … Read more