Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28383 Discussions

Issues running a program with an input file

paulo_moreira
Novice
823 Views

Hello everyone,

 

I would appreciate your help on executing an external program, which reads an input file. I am trying the following command line, with some variations:

 

call execute_command_line("C:>Program Files> ... >file.exe && input.dat")

 

When I run this program directly from the command prompt, I first enter the .exe file and then the .dat file. However I am strugling to add this to my code. Is this just a sintaxe error in the command line or should I try something different? My VS version is VS 2022 17.3.4.

 

Best regards,

 

Paulo

 

 

0 Kudos
1 Solution
jimdempseyatthecove
Honored Contributor III
815 Views

Does the program expect the input file to be a command line argument?

 

   SomePath\program.exe input.dat

Use:

   call execute_command_line("SomePath\program.exe input.dat")

 

Or does the program expect as keyboard input the file name of the input file?

 

   SomePatprogram.exe

   input.dat

 

Use:

   call execute_command_line("SomePath\program.exe<input.dat")

 

*** Note, in both formats above, the file input.dat is presumed to be located in the current directory (whatever that is). You may need to be explicit in either setting the current directory before the call, or, explicitly include the path to the input.dat file as part of the file specification.

 

BTW the && will attempt to execute input.dat should your file.exe return without error.

 

Jim Dempsey

View solution in original post

6 Replies
jimdempseyatthecove
Honored Contributor III
816 Views

Does the program expect the input file to be a command line argument?

 

   SomePath\program.exe input.dat

Use:

   call execute_command_line("SomePath\program.exe input.dat")

 

Or does the program expect as keyboard input the file name of the input file?

 

   SomePatprogram.exe

   input.dat

 

Use:

   call execute_command_line("SomePath\program.exe<input.dat")

 

*** Note, in both formats above, the file input.dat is presumed to be located in the current directory (whatever that is). You may need to be explicit in either setting the current directory before the call, or, explicitly include the path to the input.dat file as part of the file specification.

 

BTW the && will attempt to execute input.dat should your file.exe return without error.

 

Jim Dempsey

Steve_Lionel
Honored Contributor III
811 Views

I suspect the problem here is that the path to the executable contains spaces - this means that the path as seen by the command processor has to be enclosed in "", and THAT means that you have to use apostrophes to delimit the string in Fortran (or double the quotes). For example:

call execute_command_line('"C:\Program Files\Program\file.exe" && input.dat')

I would recommend specifying the full path to the input file as it might take the default from the executable location. Best is to try the command from a command prompt to make sure it works the way you want.

paulo_moreira
Novice
778 Views

Thanks, Jim and Steve.

 

The program expects a keybord input of the file name, as asked by Jim. And there was also a blank space issue, as suggested by Steve. I tried using the command line:

 

call execute_command_line('"C:\Program Files\ ... \mx202023.exe"<input.dat')

 

In this case, it just read the first line of "input.dat" file. I thus created a file with the name of the file I really want to run, and it worked propperly. Something I didn't like was that I had to create an extra file with a single line. Is it possible to make it simpler, by using a string in the command line?

 

best regards

0 Kudos
Arjen_Markus
Honored Contributor I
745 Views

Sure, that is possible:

 

call execute_command_line( "echo " // filename // '| "program that reads the name and runs"'

 

Note the "| ", the pipe character, it has the effect of redirecting standard output to the standard input in the program/command appearing after the pipe. It is a classic idiom in UNIX/Linux and it also works in a plain command window on Windows (I just checked ;)).

paulo_moreira
Novice
729 Views

That worked perfectly! Thank you all for your help and attention.

0 Kudos
Steve_Lionel
Honored Contributor III
765 Views

It sounds as if the program you're running reads a filename from standard input. If so, I don't know how you'd work around that. Do you have the source to the program?

0 Kudos
Reply