Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not real familiar with anything beyond FORTRAN 77 but here are a few pointers (and a working version of what you posted in your answer). First the working code (I added line numbers):</p> <pre><code>1 REAL FUNCTION myfile(unit, file, rows, columns) 2 IMPLICIT NONE 3 INTEGER, INTENT(IN) :: unit, rows, columns 4 CHARACTER(LEN=*) :: file 5 REAL, DIMENSION (1:columns, 1:rows) ::X 6 OPEN(unit, FILE=file, STATUS='OLD', ACTION='READ') 7 READ(unit,*), X 8 PRINT*, X 9 CLOSE(unit) 10 myfile= 0 11 END FUNCTION myfile 12 13 PROGRAM gain 14 errno = myfile(1, "test.out", 8, 4) 15 END PROGRAM </code></pre> <p>The differences are:</p> <ul> <li>line 6 - You needed to remove the quotes around 'file' in the FILE='file' assignment. As written you were using a file named file instead of the name passed in as the file parameter.</li> <li>line 10 - since you declared this as a function you need to assign a return value to the function name before you leave the function. I just assigned it a value of 0 to allow it to compile. I think what you want here is to pass the array you read in from the file back out of the routine. In that case you'll need to modify the type of the function (and assign X to myfile) or pass the array in as a parameter and allow it to be modified in the function. (in the old FORTRAN 77 world this was done with common blocks or pointers to the array, not sure how you do it in later versions of Fortran).</li> <li>line 14 - you need to assign the return value of the function to a variable. At least you did with my gfortran compiler. It wouldn't let me compile the program otherwise.</li> <li>line 14 (again) - the function call needs the name of the file (test.out) in quotes. You had it without quotes and so was having problems (this might be where your array reference error was coming from, I got different errors with my compiler.)</li> </ul> <p>Instead of using a function, you could define this your myfile routine as a subroutine instead. In this case you would definitely need to pass in the array you want filled as a parameter. You wouldn't need line 10 and instead of assigning the return value to a variable in the main program you would 'call' the routine. i.e. line 14 would look like this:</p> <pre><code>call myfile(1, "test.out",8,4) </code></pre> <p>EDIT: I posted this and then realized I forgot to answer the original questions. I did so and then for some reason couldn't connect to SO to upload the edits. So here they are finally.</p> <p>That gets your routine compiling. To actually deal with the comment lines, you have several options (at least these are the ones that come to mind initially). These are in order from simplest/most brittle to more robust/general:</p> <ol> <li>If you know exactly how many comment lines are in the data file (and it is the same for all the files) you could just read in that many lines, throw away what was read in and then read in the array from that point. This will advance you past the comments and then read in the array. However if the number of comment lines is different in the different input files, this will not work. Likewise if it changes in the future it will require changing the code. This option is probably not the best.</li> <li>Make a pass through the file read a line at a time as a string and checking to see if it starts with a # mark. If so increment a counter. When you find the first non-comment line, stop, reset the file to the beginning and then follow the steps in #1 above where you use the counter value as the number of lines to skip. This is more flexible than #1 in that it can handle an arbitrary number of comment lines but they still have to be at the beginning of the file. Any comments in the middle of the data will mess you up.</li> <li>Read in each line as a string, look for the # symbol and if it is not there, parse the line and populate the array manually. This is the most complex but gives the most flexibility for the input file format. It allows you to have (and ignore) comments anywhere in the data file.</li> </ol> <p>Which method you choose (and other people may have other suggestions) depends on your particular application. Good luck.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload