"command_argument_count()" always returns 0

47 views
Skip to first unread message

Wellington Martins

unread,
Jul 18, 2020, 8:27:22 PM7/18/20
to CBFortran
Hello everyone. I'm used to use C::B for C programming, but recently I had to run a Fortran code, then I changed specifications in C::B in order to do it (1-downloaded Mingw additional files such as gfortran, 2- Have set properly the environment variables path in Settings->Compiler->Toolchain executables and the rest of setting in this box). I'm aware that Fortran has several versions and I'm not sure in which version this code I want to run was written, probably a version more recent or at least equal to Fortran2003, since it has the command "get_command_argument" which I figured out was not present in f90 and f95. Said all of that I'm not sure if I need to choose "Fortran application" or "Console application" (as I used to do with C) when starting my project and if I need to do anything else besides what I already explained.
 
After errors like "At line 18 of file C:\..path..\Untitle1.f90 Fortran runtime error: End of file"  I realized that the problem in my code was related to "get_command_argument" function and I tried a simple code I posted below..

PROGRAM cmdlnsum
IMPLICIT NONE
CHARACTER(100) :: num1char
CHARACTER(100) :: num2char
REAL :: num1
REAL :: num2
REAL :: numsum

!First, make sure the right number of inputs have been provided
IF(COMMAND_ARGUMENT_COUNT().NE.2)THEN
  WRITE(*,*)'ERROR, TWO COMMAND-LINE ARGUMENTS REQUIRED, STOPPING'
  STOP
ENDIF

CALL GET_COMMAND_ARGUMENT(1,num1char)   !first, read in the two values
CALL GET_COMMAND_ARGUMENT(2,num2char)

READ(num1char,*)num1                    !then, convert them to REALs
READ(num2char,*)num2

numsum=num1+num2                        !sum numbers
WRITE(*,*)numsum                        !write out value

END PROGRAM

It always stops the program after accessing the first "if" statement. I verified and my "command_argument_count ()" returns 0 what I think is not supposed to happen, it was supposed to return 2, if I understood Fortran correctly so far.. I would appreciate if anyone of you guys could help me to really make this function "command_argument_count()" work. 

I can run a simpler code from where I verify my "command_argument_count()" returns me 0

program test_command_argument_count
integer :: count
count = command_argument_count()
print *, count
end program test_command_argument_count

Build Log:

Warning: Nonexistent include directory 'C\..path..\Untitled1.os_output_dir'

Thank you all in advance!


P.S.: First time posting and I did not find any post template (if there is any). If I need to rewrite my post let me know my steps. In our discussions if you find it better I can show first the true code I want to run.

Geordie

unread,
Jul 18, 2020, 9:12:02 PM7/18/20
to CBFortran
Hello mate,

How are you calling the program?  Should be something like:

cmdlssum.exe 12.04 73.81

I generally use the gcc Fortran iargc() and getarg() statements for command line arguments (https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran/IARGC.html).

Cheers,

Geordie.

Wellington Martins

unread,
Jul 18, 2020, 9:41:03 PM7/18/20
to CBFortran
Hello! By the way I'm on Windows 10, I forgot to mention.. Ok, I just write it in the C::B window (after creating an empty file ) and press F9 to compile and run. I'm aware there is a file generated with extension .exe but I generally don't worry about it. I just checked and its name is correct, but I'm not sure what you mean by "12.04 73.81"  tried to include it in the file name and had no effect.

I also checked the link you've sent me and in the example line there is this link https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran/GETARG.html#GETARG from where I copied- pasted the example and ran it successfully (but I think something is wrong). I thought the example code in this link was supposed to ask me to type a number to use it in the loop (I'm actually regarding iargc() in this example as "scanf" function in C). The whole point is this, either with my original code or with this simple one to fill arguments in the code from the command line, but looks like there is something missing.

Thank you for your kind response, 

Regards,
Wellington

Geordie

unread,
Jul 18, 2020, 9:58:09 PM7/18/20
to CBFortran
Ah, I see!

Command line arguments are passed to the EXE at the command line and COMMAND_ARGUMENT_COUNT() counts them.  The two numbers in my example represent argument 1 and 2 that you're checking for in your program.

Yeah, you don't need the .EXE to run the program, but I wanted to make the syntax as clear as possible.

c:\> program.exe <arg 1> <arg 2> ... <arg n>

where COMMAND_ARGUMENT_COUNT() will return N


Wellington Martins

unread,
Jul 19, 2020, 8:26:14 AM7/19/20
to cbfo...@googlegroups.com

After running my code a .exe file is automatically generated, so how I'm supposed to name it like you have described? I tried to name my .f90 file like that but it didn't work, did the same with the .exe file put the terminal window pop up too fast and I can't see the result even after adding "pause" command to my code. I also tried writing the first line of my code in the way you've described like "program cmdlnsum <12.04> <7.81>" but I got an error like "Invalid form of PROGRAM statement", which for me makes total sense, just the compiler saying that what I tried is stupid. I'm guessing I need to change something in compliler settings don't you think?

[]'s
WM

--
You received this message because you are subscribed to the Google Groups "CBFortran" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cbfortran+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cbfortran/b96396a3-2569-44d6-ae70-9f1a3c44c73fo%40googlegroups.com.

Geordie

unread,
Jul 19, 2020, 9:36:18 AM7/19/20
to CBFortran
What you need to do is open a command window (Start> type CMD into the search, run it) then change to the directory where a.exe is, i.e. cd c:\fortran\repos\cmdlnsum, then run a.exe followed by two real values which are the command line arguments.  You should be fine from there.

Use PowerShell if you're comfortable with that, and you can open a PS window at the location of interest by shift-right clicking on the folder and selecting "Open PowerShell window here..."


On Sunday, 19 July 2020 20:26:14 UTC+8, Wellington Martins wrote:

After running my code a .exe file is automatically generated, so how I'm supposed to name it like you have described? I tried to name my .f90 file like that but it didn't work, did the same with the .exe file put the terminal window pop up too fast and I can't see the result even after adding "pause" command to my code. I also tried writing the first line of my code in the way you've described like "program cmdlnsum <12.04> <7.81>" but I got an error like "Invalid form of PROGRAM statement", which for me makes total sense, just the compiler saying that what I tried is stupid. I'm guessing I need to change something in compliler settings don't you think?

[]'s
WM

Em sáb., 18 de jul. de 2020 às 22:58, Geordie <geordie...@gmail.com> escreveu:
Ah, I see!

Command line arguments are passed to the EXE at the command line and COMMAND_ARGUMENT_COUNT() counts them.  The two numbers in my example represent argument 1 and 2 that you're checking for in your program.

Yeah, you don't need the .EXE to run the program, but I wanted to make the syntax as clear as possible.

c:\> program.exe <arg 1> <arg 2> ... <arg n>

where COMMAND_ARGUMENT_COUNT() will return N


--
You received this message because you are subscribed to the Google Groups "CBFortran" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cbfo...@googlegroups.com.

Darius Markauskas

unread,
Jul 19, 2020, 1:36:22 PM7/19/20
to cbfo...@googlegroups.com
" ...the terminal window pop up too fast and I can't see the result ..."
Check if on the "Project/targets options" dialog "Pause when execution ends" is selected. See screenshot 1.

I am unsure if and how you added program arguments. Program arguments in C::B are added using menu: Project->Set programs arguments. See screenshot 2.
screenshot_1.png
screenshot_2.png


To unsubscribe from this group and stop receiving emails from it, send an email to cbfortran+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cbfortran/8007b628-746d-4c2f-9992-d7bdf9f96f43o%40googlegroups.com.

Wellington Martins

unread,
Jul 19, 2020, 5:21:11 PM7/19/20
to cbfo...@googlegroups.com
Man! Perfect! With your advice, I managed to run all codes I wanted. Just a simple answer that I could not find online. Huge "Thank you !"

P.S.: Have found this discussion group amazing! 

Cheers, 
WM 

To unsubscribe from this group and stop receiving emails from it, send an email to cbfortran+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cbfortran/8007b628-746d-4c2f-9992-d7bdf9f96f43o%40googlegroups.com.

Wellington Martins

unread,
Jul 19, 2020, 5:23:27 PM7/19/20
to cbfo...@googlegroups.com
Great Darius, I will try what you have told me but as I said in a response to Geordi my main issue was solved. Thank you also!

You all have a nice week!
WM 

Reply all
Reply to author
Forward
0 new messages