tags:

views:

46

answers:

2

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 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...

A: 

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.");
            }
Cipi
Still, the process can't read/write information from/to files in the same directory correctly...
Alex
How are you starting it? You must provide absolute paths to the files you want to read/write... well I tried to help. =)
Cipi
What is the value of `ExecutableFileName`?
Cipi
Yes, the path to the process is absolute
Alex
No the path to the process, but the paths to the files. If you must use relative file paths, you can set `p.StartInfo.WorkingDirectory = "C:\\My Dir";` attribute, so relative paths get calculated relative to the working dir.
Cipi
+1  A: 

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.

Roman Kuzmin