How to redirect standard input and standard output using .Net System.Dia​gnostics.P​rocess

22 ビュー (過去 30 日間)
I'm working on some optimisation code and have recently moved away from using the system() function to open xfoil.exe and direct input and output files,
cmd = sprintf('cd %s && xfoil.exe < xfoil.inp > xfoil.out &',working_directory);
[status,result] = system(cmd);
and I am now attempting to complete the same operations using:
process = System.Diagnostics.Process();
process.StartInfo.FileName = 'xfoil.exe';
process.StartInfo.Arguments = %not sure what to put here%;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.Start();
I'm not very experienced with coding in general, so I'm struggling a bit here, but I really need some help to figure out how to redirect standard input and standard output to my two files (xfoil.inp and xfoil.out) using .Net System.Diagnostics.Process.

採用された回答

Mohammad Sami
Mohammad Sami 2020 年 8 月 31 日
You can try this. You may have to choose the option on how to decide when xfoil.exe has finished reading.
process = System.Diagnostics.Process();
process.StartInfo.FileName = 'xfoil.exe';
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = false;
process.Start();
stdin = process.StandardInput;
stdout = process.StandardOutput;
fid = fopen('xfoil.inp','r'); % read the xfoil.inp
txt = fread(fid,'*char');
fclose(fid);
stdin.Write(txt); % send the input to xfoil.exe
out = stdout.ReadToEndAsync;
% wait for xfoil.exe to finish processing
% option 1
pause(1); % pause for a predefined amount of time
% option 2
% wait until xfoil.exe has terminated
while(~process.HasExited)
pause(1);
end
try
process.Kill; % kill the process if needed.
catch
end
xfoil_out = char(out.Result);
  10 件のコメント
Michael Maurice
Michael Maurice 2020 年 9 月 2 日
編集済み: Michael Maurice 2020 年 9 月 2 日
I think i got it! Standard input needs to be closed. I added "stdin.Close()'. This is the updated code, which is now delivering all input data from "xfoil.inp" to xfoil.exe:
%input data
fid_in = fopen('xfoil.inp','r'); % read the xfoil.inp
while ~feof(fid_in)
input = fgetl(fid_in);
disp(input)
stdin.WriteLine(input); % send the input to xfoil.exe
end
stdin.Close()
Mohammad Sami
Mohammad Sami 2020 年 9 月 2 日
Good to know it's working for you and the final solution.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGas Dynamics についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by