Here my situation. I'm still pretty new at this. I'm generating some .txt files in my linux virtual box and using a c# winform application in windows to load the data into a chart. There are around 20 files or so. What I need to happen is to load the first file, display it on the chart, reload the file and display it on the chart, reload the file, display it on the chart, etc. My c code is writing over the same file every time with new values. So in the winforms when you are looking at the chart you will kind of see the data transforming as each new dataset is loaded. This should happen fairly quickly. Some of the new data loads but then I get the error about the file being used by another process. I've been reading about the error but I guess I still don't understand how to fix it. I need my c code that is generating the files to line up with the c# chart loading code so that it happens smoothly. In C I am also getting an error message that the file couldn't be opened for writing.
Here is what I'e tried so far. I tried waiting for a moment in the c code before opening the file for writing which didn't work. I've also tried just trying to open the file repeatedly until it opens without error, but it didn't work. My only other solution I could think of was to just create files like 1.txt 2.txt 3.txt... and then load them in succession. That might be the way I have to go. But I'm still new and don't actually know if I'm doing this the best way to start.
C code
for(i = 0; i < FILES; i++){
fd = fopen(path, "w+");
if( fd == NULL ){
printf( "Could not open file for writing. Exiting...\n");
exit(-1);
}
for(j = 0; j < ROW; j++){
fprintf(fd, "%.4f", vector[i][j]);
fprintf(fd, "%s", "\n");
}
fclose(fd);
}
C# Code
// Define the event handlers.
private void OnChanged(object source, FileSystemEventArgs e)
{
FinalList.Clear();
chart4.Series[0].Points.Clear();
loadData(FinalPath, FinalList, chart4);
}
private void loadData(String path, List<double> list, System.Windows.Forms.DataVisualization.Charting.Chart chart)
{
string line = null;
double value = 0;
using (TextReader reader = File.OpenText(path))
{
line = reader.ReadLine();
while (line != null)
{
value = System.Convert.ToDouble(line);
list.Add(value);
line = reader.ReadLine();
}
}
for (int i = 0; i < ROW; i++)
{
chart.Series["Graph"].Points.AddXY(i + 1, list[i]);
}
}