Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing filename from C++ to Fortran

Passing filename from C++ to Fortran

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++linuxquestion
27 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R RedSonja

    What happened? When I call my little ftest.exe from the debugger or from the command window it works, I have to enter the two strings by hand. I can see that it has filled the input strings up to 80 chars, but still manages to open the file. When I call it from C++ it fills the strings up to 80 chars, but can't open the file. I tried terminating with \n, with \r\n, and with \r\n\0, it all made no difference. There must be something else. This is very strange. READ(5,'(A)',IOSTAT=IOS) FILEN It must be the '(A)'

    ------------- Bibo ergo sum

    modified on Tuesday, December 9, 2008 8:22 AM

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #12

    Hi, AFAIK '(A)' just means "take any text" I suggest you: - make sure your debugging stuff visualizes all special chars including space, NULL, CR, LF - display the status of both openfile operations, I mean the actual value so you can google it (a >0 test is not sufficient) - try the Fortran test app manually with different paths - show us all the relevant Fortran code :)

    Luc Pattyn [Forum Guidelines] [My Articles]


    Fixturized forever. :confused:


    R 1 Reply Last reply
    0
    • L Luc Pattyn

      Hi, AFAIK '(A)' just means "take any text" I suggest you: - make sure your debugging stuff visualizes all special chars including space, NULL, CR, LF - display the status of both openfile operations, I mean the actual value so you can google it (a >0 test is not sufficient) - try the Fortran test app manually with different paths - show us all the relevant Fortran code :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      Fixturized forever. :confused:


      R Offline
      R Offline
      RedSonja
      wrote on last edited by
      #13

      This is the entire code: PROGRAM FTEST IMPLICIT NONE C INTEGER IOS CHARACTER*80 FILEN, FLBASE C C CONTINUE READ(5,'(A)',IOSTAT=IOS) FILEN IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A FILE NAME' WRITE (6) 'FILEN = *', FILEN, '*' READ(5,'(A)',IOSTAT=IOS) FLBASE IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A ROOT FILE NAME' WRITE (6) 'FLBASE = *', FLBASE, '*' OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD', $ READONLY,IOSTAT=IOS) IF ( IOS .NE. 0 ) THEN WRITE (6, FMT=*) 'ERROR NO INPUT FILE ', IOS ELSE WRITE (6) 'OPENED INPUT FILE OK' ENDIF C CONTINUE END

      ------------- Bibo ergo sum

      modified on Tuesday, December 9, 2008 8:46 AM

      L 1 Reply Last reply
      0
      • R RedSonja

        This is the entire code: PROGRAM FTEST IMPLICIT NONE C INTEGER IOS CHARACTER*80 FILEN, FLBASE C C CONTINUE READ(5,'(A)',IOSTAT=IOS) FILEN IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A FILE NAME' WRITE (6) 'FILEN = *', FILEN, '*' READ(5,'(A)',IOSTAT=IOS) FLBASE IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A ROOT FILE NAME' WRITE (6) 'FLBASE = *', FLBASE, '*' OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD', $ READONLY,IOSTAT=IOS) IF ( IOS .NE. 0 ) THEN WRITE (6, FMT=*) 'ERROR NO INPUT FILE ', IOS ELSE WRITE (6) 'OPENED INPUT FILE OK' ENDIF C CONTINUE END

        ------------- Bibo ergo sum

        modified on Tuesday, December 9, 2008 8:46 AM

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #14

        OK, now 1. what is the result opening the first file? is it ERROR ... or OK on first file? 2. where is the opening of the second file? and what is its result? :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        Fixturized forever. :confused:


        R 1 Reply Last reply
        0
        • L Luc Pattyn

          OK, now 1. what is the result opening the first file? is it ERROR ... or OK on first file? 2. where is the opening of the second file? and what is its result? :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Fixturized forever. :confused:


          R Offline
          R Offline
          RedSonja
          wrote on last edited by
          #15

          It gives me an IOS=2 on the first file. I will worry about the second string another day, it gets used to build some complicated output filenames.

          ------------- Bibo ergo sum

          L 2 Replies Last reply
          0
          • R RedSonja

            It gives me an IOS=2 on the first file. I will worry about the second string another day, it gets used to build some complicated output filenames.

            ------------- Bibo ergo sum

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #16

            Hi, Not sure but maybe IOS=2 means "the file you want to read exists but is empty". Please check. I expect IOS=6 for "no such file", IOS=0 for anything else (until you start working on the file that is) unless the filename is invalid (not sure which code gets emited then). You could try creating and writing the file, rather than opening and reading; that way, Explorer (or DOS DIR) would show the effective filename. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Fixturized forever. :confused:


            1 Reply Last reply
            0
            • R RedSonja

              It gives me an IOS=2 on the first file. I will worry about the second string another day, it gets used to build some complicated output filenames.

              ------------- Bibo ergo sum

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #17

              Hi, I installed Silverfrost F95 and was able to run this:

              PROGRAM FTEST
              IMPLICIT INTEGER(I-N)

              INTEGER IOS
              CHARACTER*80 FILEN, FLBASE
              CHARACTER C

              PRINT *,'enter filename1'
              READ(5,'(A)',IOSTAT=IOS) FILEN
              IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A FILE NAME'
              PRINT *, 'FILEN = *', FILEN, '*'
              DO 10 I = 1,80
              C=FILEN(I:I)
              IF (ICHAR(C).NE.32) PRINT *, 'FILEN[',I,']=',C,'=',ICHAR(C)
              10 CONTINUE

              PRINT *,'enter filename2'
              READ(5,'(A)',IOSTAT=IOS) FLBASE
              IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A ROOT FILE NAME'
              PRINT *, 'FLBASE = *', FLBASE, '*'
              DO 20 I = 1,80
              C=FLBASE(I:I)
              IF (ICHAR(C).NE.32) PRINT *, 'FLBASE[',I,']=',C,'=',ICHAR(C)
              20 CONTINUE

              OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)
              IF ( IOS .NE. 0 ) THEN
              PRINT *, 'ERROR NO INPUT FILE 1 ', IOS
              ELSE
              PRINT *, 'OPENED INPUT FILE 1 OK'
              ENDIF
              OPEN(18,FILE=FLBASE,FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)
              IF ( IOS .NE. 0 ) THEN
              PRINT *, 'ERROR NO INPUT FILE 2 ', IOS
              ELSE
              PRINT *, 'OPENED INPUT FILE 2 OK'
              ENDIF

              END

              it gives OK for existing files, and error 128 for non-existing files. The documentation says error codes could be different on different compilers. So it is my guess something is wrong in the way you pass the filenames from C++; anyway the above should be able to tell you what gets read. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              G R 2 Replies Last reply
              0
              • L Luc Pattyn

                Hi, I installed Silverfrost F95 and was able to run this:

                PROGRAM FTEST
                IMPLICIT INTEGER(I-N)

                INTEGER IOS
                CHARACTER*80 FILEN, FLBASE
                CHARACTER C

                PRINT *,'enter filename1'
                READ(5,'(A)',IOSTAT=IOS) FILEN
                IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A FILE NAME'
                PRINT *, 'FILEN = *', FILEN, '*'
                DO 10 I = 1,80
                C=FILEN(I:I)
                IF (ICHAR(C).NE.32) PRINT *, 'FILEN[',I,']=',C,'=',ICHAR(C)
                10 CONTINUE

                PRINT *,'enter filename2'
                READ(5,'(A)',IOSTAT=IOS) FLBASE
                IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A ROOT FILE NAME'
                PRINT *, 'FLBASE = *', FLBASE, '*'
                DO 20 I = 1,80
                C=FLBASE(I:I)
                IF (ICHAR(C).NE.32) PRINT *, 'FLBASE[',I,']=',C,'=',ICHAR(C)
                20 CONTINUE

                OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)
                IF ( IOS .NE. 0 ) THEN
                PRINT *, 'ERROR NO INPUT FILE 1 ', IOS
                ELSE
                PRINT *, 'OPENED INPUT FILE 1 OK'
                ENDIF
                OPEN(18,FILE=FLBASE,FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)
                IF ( IOS .NE. 0 ) THEN
                PRINT *, 'ERROR NO INPUT FILE 2 ', IOS
                ELSE
                PRINT *, 'OPENED INPUT FILE 2 OK'
                ENDIF

                END

                it gives OK for existing files, and error 128 for non-existing files. The documentation says error codes could be different on different compilers. So it is my guess something is wrong in the way you pass the filenames from C++; anyway the above should be able to tell you what gets read. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Fixturized forever. :confused:


                G Offline
                G Offline
                Garth J Lancaster
                wrote on last edited by
                #18

                Luc Pattyn wrote:

                I installed Silverfrost F95

                nice one Luc - thats dedication !!! 'g'

                L 1 Reply Last reply
                0
                • R RedSonja

                  I got this old Fortran exe and I have to embed in in C++. After starting it needs two filenames entered by the user. I did this by putting the filenames in files, attaching handles to them, making a Pipe and attaching it to the child process, then doing ReadFile and WriteFile to the pipe, as shown in MSDN. Anyway, it gets the strings holding the filenames. I have to append \n else Fortran won't accept it. The Fortran people advised me to try with \r before \n, it seems to grab that too. C++ can append a \0 if I let it, I tried that too. However, when it takes the first string and tries to open the file, it can't do it and it returns an error. (It returns the error tidily to my C++ process, so it can't be that bad.) If I run it directly from the command line, as the original author did, it all works fine with the same files. So there must be something wrong with the way I pass my strings. Unfortunately I can see into the Fortran code but I can't get at it to see what the problem is. We have some old Fortran compilers lying about, but they won't even compile it. The original came from Linux, can that have something to do with it? The users have real problems if they use a Windows editor, it doesn't like Windows carriage returns, they say. But I run it from the command line and it reads all kinds of other files I generated with C++. There must be someone out there who has done this before?

                  ------------- Bibo ergo sum

                  C Offline
                  C Offline
                  cp9876
                  wrote on last edited by
                  #19

                  Another possible difference to running it from the command line is the location of the working directory (I note that you seem to be using filenames and not full paths). Maybe you are now successfully transferring the filenames but the working directory is different, perhaps somewhere where the program doesn't have permission, or a file that should exist in it doesn't. The statement: OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD', $ READONLY,IOSTAT=IOS) will give you some sort of error if the file doesn't exist as STATUS='OLD' tells the program that it should be there.

                  Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                  R 1 Reply Last reply
                  0
                  • C cp9876

                    Another possible difference to running it from the command line is the location of the working directory (I note that you seem to be using filenames and not full paths). Maybe you are now successfully transferring the filenames but the working directory is different, perhaps somewhere where the program doesn't have permission, or a file that should exist in it doesn't. The statement: OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD', $ READONLY,IOSTAT=IOS) will give you some sort of error if the file doesn't exist as STATUS='OLD' tells the program that it should be there.

                    Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                    R Offline
                    R Offline
                    RedSonja
                    wrote on last edited by
                    #20

                    Yes yes yes! I put the whole path in (without doubling the \) and the test program works. It doesn't work for the original Fortran yet but at least I know where to start. If Fortran is only going to give it 80 chars every time I can see some problems coming up on the target machine, because the paths can get a lot longer. I did try a relative path but it wasn't having it, must try and get Fortran to tell me where it is. In C++ I use _getcwd, can someone tell me how to get it in Fortran? It was giving me IOS=2, which is not in the list of Intel error messages.

                    ------------- Bibo ergo sum

                    C L 2 Replies Last reply
                    0
                    • L Luc Pattyn

                      Hi, I installed Silverfrost F95 and was able to run this:

                      PROGRAM FTEST
                      IMPLICIT INTEGER(I-N)

                      INTEGER IOS
                      CHARACTER*80 FILEN, FLBASE
                      CHARACTER C

                      PRINT *,'enter filename1'
                      READ(5,'(A)',IOSTAT=IOS) FILEN
                      IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A FILE NAME'
                      PRINT *, 'FILEN = *', FILEN, '*'
                      DO 10 I = 1,80
                      C=FILEN(I:I)
                      IF (ICHAR(C).NE.32) PRINT *, 'FILEN[',I,']=',C,'=',ICHAR(C)
                      10 CONTINUE

                      PRINT *,'enter filename2'
                      READ(5,'(A)',IOSTAT=IOS) FLBASE
                      IF ( IOS .NE. 0 ) STOP 'MUST PROVIDE A ROOT FILE NAME'
                      PRINT *, 'FLBASE = *', FLBASE, '*'
                      DO 20 I = 1,80
                      C=FLBASE(I:I)
                      IF (ICHAR(C).NE.32) PRINT *, 'FLBASE[',I,']=',C,'=',ICHAR(C)
                      20 CONTINUE

                      OPEN(17,FILE=FILEN,FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)
                      IF ( IOS .NE. 0 ) THEN
                      PRINT *, 'ERROR NO INPUT FILE 1 ', IOS
                      ELSE
                      PRINT *, 'OPENED INPUT FILE 1 OK'
                      ENDIF
                      OPEN(18,FILE=FLBASE,FORM='FORMATTED',STATUS='OLD',IOSTAT=IOS)
                      IF ( IOS .NE. 0 ) THEN
                      PRINT *, 'ERROR NO INPUT FILE 2 ', IOS
                      ELSE
                      PRINT *, 'OPENED INPUT FILE 2 OK'
                      ENDIF

                      END

                      it gives OK for existing files, and error 128 for non-existing files. The documentation says error codes could be different on different compilers. So it is my guess something is wrong in the way you pass the filenames from C++; anyway the above should be able to tell you what gets read. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      Fixturized forever. :confused:


                      R Offline
                      R Offline
                      RedSonja
                      wrote on last edited by
                      #21

                      Thankyou. You have helped me immensely. It almost works now; I can run my test exe from C++, now "just" need to get the real thing running. If I discover anything new I will report it. Meanwhile I am very grateful.

                      ------------- Bibo ergo sum

                      1 Reply Last reply
                      0
                      • R RedSonja

                        Yes yes yes! I put the whole path in (without doubling the \) and the test program works. It doesn't work for the original Fortran yet but at least I know where to start. If Fortran is only going to give it 80 chars every time I can see some problems coming up on the target machine, because the paths can get a lot longer. I did try a relative path but it wasn't having it, must try and get Fortran to tell me where it is. In C++ I use _getcwd, can someone tell me how to get it in Fortran? It was giving me IOS=2, which is not in the list of Intel error messages.

                        ------------- Bibo ergo sum

                        C Offline
                        C Offline
                        cp9876
                        wrote on last edited by
                        #22

                        To find out where you are, GNU Fortran has a GETCWD call link[^] I don't know how standard it is. It doesn't seem to have a corresponding SETCWD call. If you are starting the program using CreateProcess() then you can set the working directory, and I think it will default to the working directory of the calling process if you don't.

                        Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                        R 1 Reply Last reply
                        0
                        • C cp9876

                          To find out where you are, GNU Fortran has a GETCWD call link[^] I don't know how standard it is. It doesn't seem to have a corresponding SETCWD call. If you are starting the program using CreateProcess() then you can set the working directory, and I think it will default to the working directory of the calling process if you don't.

                          Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                          R Offline
                          R Offline
                          RedSonja
                          wrote on last edited by
                          #23

                          Unfortunately the Absoft compiler is a bit basic and does not have such luxuries. But I must check what I put in CreateProcess. I have to put it all in the command line, maybe I missed the path and it was just luck that it ran where it is.

                          ------------- Bibo ergo sum

                          C 1 Reply Last reply
                          0
                          • R RedSonja

                            Yes yes yes! I put the whole path in (without doubling the \) and the test program works. It doesn't work for the original Fortran yet but at least I know where to start. If Fortran is only going to give it 80 chars every time I can see some problems coming up on the target machine, because the paths can get a lot longer. I did try a relative path but it wasn't having it, must try and get Fortran to tell me where it is. In C++ I use _getcwd, can someone tell me how to get it in Fortran? It was giving me IOS=2, which is not in the list of Intel error messages.

                            ------------- Bibo ergo sum

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #24

                            Hi, if pathname length is a concern, you can always "map a network drive" (it works on the local machine too, despite the name), i.e. assign a drive letter to a (deep) subdirectory, allowing you to shorten the pathname considerably. :)

                            Luc Pattyn [Forum Guidelines] [My Articles]


                            Fixturized forever. :confused:


                            1 Reply Last reply
                            0
                            • G Garth J Lancaster

                              Luc Pattyn wrote:

                              I installed Silverfrost F95

                              nice one Luc - thats dedication !!! 'g'

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #25

                              My very first large apps were coded in Fortran IV, some 30 years ago, but I hadn't done any Fortran work since I switched to C, also long ago. This was an opportunity to read some of the docs, and experiment a bit. :)

                              Luc Pattyn [Forum Guidelines] [My Articles]


                              Fixturized forever. :confused:


                              1 Reply Last reply
                              0
                              • R RedSonja

                                Unfortunately the Absoft compiler is a bit basic and does not have such luxuries. But I must check what I put in CreateProcess. I have to put it all in the command line, maybe I missed the path and it was just luck that it ran where it is.

                                ------------- Bibo ergo sum

                                C Offline
                                C Offline
                                cp9876
                                wrote on last edited by
                                #26

                                Check out the lpCurrentDirectory parameter to the CreateProcess call

                                Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                                1 Reply Last reply
                                0
                                • R RedSonja

                                  I got this old Fortran exe and I have to embed in in C++. After starting it needs two filenames entered by the user. I did this by putting the filenames in files, attaching handles to them, making a Pipe and attaching it to the child process, then doing ReadFile and WriteFile to the pipe, as shown in MSDN. Anyway, it gets the strings holding the filenames. I have to append \n else Fortran won't accept it. The Fortran people advised me to try with \r before \n, it seems to grab that too. C++ can append a \0 if I let it, I tried that too. However, when it takes the first string and tries to open the file, it can't do it and it returns an error. (It returns the error tidily to my C++ process, so it can't be that bad.) If I run it directly from the command line, as the original author did, it all works fine with the same files. So there must be something wrong with the way I pass my strings. Unfortunately I can see into the Fortran code but I can't get at it to see what the problem is. We have some old Fortran compilers lying about, but they won't even compile it. The original came from Linux, can that have something to do with it? The users have real problems if they use a Windows editor, it doesn't like Windows carriage returns, they say. But I run it from the command line and it reads all kinds of other files I generated with C++. There must be someone out there who has done this before?

                                  ------------- Bibo ergo sum

                                  R Offline
                                  R Offline
                                  RedSonja
                                  wrote on last edited by
                                  #27

                                  Fixed it! I finally ported the old Fortran to Windows and recompiled it. I put some prints in to see what it gets up to. It works. The problem was the Fortran having been compiled under Cygwin, some kind of Linux for Windows. It ran under Windows, but not quite. So when I compiled it myself, it could use the strings to open the file. Just because it runs from the command line does not mean you can embed it and it still runs.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups