I have two programs working together to write and read data from a file.
The first program is a C# Form. It takes in a file name from the user and writes it out to a file on the C drive and exits/closes the C# application.
Revelvant code:
private void startBtn_Click(object sender, System.EventArgs e)
{
using (StreamWriter writer = new StreamWriter("C:\\File.txt"))
{
writer.WriteLine(fileName.Text + ".txt");
writer.Close();
}
Application.Exit();
}
The second program in C++. This program creates a C# application only when the f10 is pressed and then halts until the C# application closes. Then the C++ program resumes and reads in the file name that was wrote to the file on the C drive. The C++ program is highly specialized/customized and additions to it have to be handled carefully.
Relevant Code:
std::string path("C:\\CSharpApp.exe");
system(path.c_str()); //Halts here until CSharpApp exits
if(!reader.is_open()) //Reader is a global ifstream object
{
reader.open("C:\\File.txt");
}
char temp[MAX_FILE_NAME];//250
reader >> temp;
reader.close();
string temp2 = temp;
//cout << temp2 << endl; //Using to debug
FileName = "C:\\data\\" + temp2;//temp2 contains a file name ending with .txt
if(!writer.is_open())//Writer is a global ofstream object
{
writer.open(FileName.c_str(),ios::app);
}
//write some stuff to file
writer.close();
Pressing the f10 key the first time invokes the C++ code and it calls the C# app and the name of the file is stored correctly. The C++ program resumes when the CSharpApp closes and the file name is read correctly.
Problem:
Any additional f10 key presses result in the cout statement showing that temp2 has a value of nothing, I just see the endl in the console. The File.txt contains the correct name of the next file but for some reason the C++ program only gets the file name correctly the first time f10 is pressed.
The purpose of this functionality is to press f10 and create a new file with a new name each time f10 is pressed.
I don’t think both programs can access the file at the same time because the execution of the C# app halts the C++ program. Also I tried changing the writer inside the CSharp application to append the filenames rather then overwrite the file. This caused the C++ application to print out temp2 as being the first file name only, and no blank lines. Not sure what this means.
I am currently working to solve this and will update as I make progress.
UPDATE:
I added the following in place of: reader >> temp;
Code:
if(reader >> temp)
cout << "Read in ok" << endl;
else
cout << "PROBLEM OCCURRED" << endl;
Now my program is functioning correctly, however I am not clear as to why it is working now. To me I’m assuming that adding this if test and cout statements must have given the program more time.
Thanks for the help
I am suspicious that the file write in the C# app is not finished committing to disk before your C++ app is trying to read it. You might try adding writer.Flush() before the call to writer.Close() and see if it makes any difference.