开发者

System.Diagnostics.Process: when I run a C++ program that creates an output file and writes something, the output file is not created

When I run a C++ program that creates an output file and writes something, the output file is not created, although the program works fine when I simply double click it from开发者_Go百科 Windows Explorer.

This is the C# code I use to run the program:

            try
            {
                Process p = StartProcess(ExecutableFileName);
                p.Start();
                p.WaitForExit();
                Log("Program finished in " + ((p.ExitTime - p.StartTime).Milliseconds / 1000m) + " seconds with code " + p.ExitCode + "\n");
            }
            catch
            {
                Log("The program couldn't be started.");
            }

UPDATE

I just found out why it's happening.

Apparently, when I launch it with C#, the C++ program doesn't see the input file in the relative directory, but when I explicitly specify it

ifstream in("C:\\Alex\\primes.in");

it gets it and everything works! Now I need to make it work with relative file paths...


Here is the summary of our discussion of the problem. It turned out that the output file was in the C# program's debug folder, not in the directory where the C++ application was and the output was expected. The problem is solved by specifying the Working directory property of the project.


You must call Close() on process like so:

            try
            {
                Process p = StartProcess(ExecutableFileName);
                p.Start();
                p.WaitForExit();

                //THIS HERE
                p.Close();                    

                Log("Program finished in " + ((p.ExitTime - p.StartTime).Milliseconds /1000m) + " seconds with code " + p.ExitCode + "\n");


            }
            catch
            {
                Log("The program couldn't be started.");
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜