Using variables to open output files in Fortran - string

I have a piece of Fortran code:
C --------------------------------------------
CALL READIN_HYD
CALL READIN_CONFIG
CALL READIN_FORCE
CALL READIN_STEPPER
C --------------------------------------------
OPEN(11,FILE='EVO_0wall.dat')
and I'm attempting to replace the hardcoded file name (EVO_0wall.dat) with something I can input from my input parameters (which are all read by the subroutines readin_hyd, etc).
I'm trying to do something like this:
CHARACTER*30 OUTFILE
WRITE(*,*) 'OUTPUT FILE'
READ(*,*) OUTFILE
WRITE(*,*) 'OUTPUT FILE: ',OUTFILE
which I have added into the READIN_CONFIG subroutine. Coming back, I replace with
OPEN(11,FILE=OUTFILE,STATUS='NEW')
in the main routine in the hope that it will say the same thing as before if the input file I pipe in contains 'EVO_0wall.dat' (with the apostrophes) in the appropriate place.
If I run the code, all other input variables are read correctly, and the data is output correctly - however, it creates and places the output in an odd file with no extension and broken characters for a name (for example, degree, "{a}, and 0 ). Renaming the file with a .dat extension lets me open it, and the data within is correct. (edit: actually, the variable OUTFILE changes to the odd characters when its in the main function, if I try to simply print its value, so I guess its not just wrong syntax in the OPEN statement)
Is there some way that Fortran handles strings that I'm missing between these? I'm afraid I'm a novice to Fortran (this is someone else's code that I'm adapting), and am not quite sure what I'm missing. Any help would be much appreciated!
Thanks!

As an alternative to the suggestion of #M.S.B., you may use trim and adjustl, like this:
implicit none
character*99 :: outf
outf='outf.outf'
open(1,file=trim(adjustl(outf)))
write(1,*)'foobar',42
close(1)
end
Here adjustl ensures that there's no leading whitespace, and trim trims the trailing whitespace. This should work as long as the outf variable only contains legal characters [I'd suggest using basic ASCII only].
On a side note, if I add status='new' to the open statement, it fails at runtime (with gfortran 4.4.3 at least) if the file already exists. If you really want to make sure that the existing file is not overwritten, you need to inquire first.

That seems OK. The following read statement should allow you to omit the apostrophes:
read (*, '(A)' ) outfile
What does the "echo" write statement of the value of outfile output?

My FORTRAN is rusty, but I think the {a} may represent hex A, or decimal 10, which is a newline. That's what you would get when you read in the OUTFILE name.
You may need to TRIM the OUTFILE of all whitespace

Related

Why does my code run successfully but give not output?

I am new to python. I am trying to open a text file with name 'P' and show the lines as an output. I wrote the code below, and it runs but not output. Why is this so?
with open('/Users/LENOVO/Desktop/P.txt','rt') as a_file:
for lines in a_file.readlines():
print(lines,ends='')`
because your print contains end=""
usually the \n is used to flush the output.
if you want to force it to flush the buffer (so to print it without the \n) you can add the flush option :
print(lines,end='', flush=True)
But maybe you just want to remove the end keyword:
print(lines)
When to use end=''?
When you use the end parameter you replace the usual \n (carret return) by something else, that is useful if you want to display a série of things next to each other without going next line everytime. (for instance if you want to print a dot . or x for each call to a function that you're calling in a loop for a big number of times.
And yes it should be end and not ends
See reference here :
https://www.w3schools.com/python/ref_func_print.asp
I do not know where you got ends='' from, but if you remove it, it produces an output.
I also figured I would also add that you are most likely referring to end, this will essentially just replace the \n which is the default value with whatever you wish.

Is there an end= equivalent for inputs?

So as I'm sure you know there's a specific operator for print() functions called end.
#as an example
print('BOB', end='-')
#would output
BOB-
So is there something like this for inputs? For example, if I wanted to have an input that would look something like this:
Input here
►
-------------------------------------------------------
And have the input at the ► and be inside the dashes using something like
x = input('Input here\n►', end='-------')
Would there be some equivalent?
EDIT:
Just to be clear, everything will be printed at the same time. The input would just be on the line marked with the ►, and the ---- would be printed below it, but at the SAME time. This means that the input would be "enclosed" by the ---.
Also, there has been a comment about curses - can you please clarify on this?
Not exactly what you want, but if the --- (or ___) can also be on the same line, you could use an input prompt with \r:
input("__________\r> ")
This means: print 10 _, then go back \r to the beginning of the line, print > overwriting the first two _, then capture the input, overwriting more _. This shows the input prompt > ________. After typing some chars: > test____. Captured input: 'test'
For more complex input forms, you should consider using curses.
When using basic console IO, once a line has been ended with a newline, it's gone and can't be edited. You can't move the cursor up to do print anything above that last line, only add on a new line below.
That means that without using a specialized "console graphics" library like curses (as tobias_k suggests), you pretty much can't do what you're asking. You can mess around a little with the contents of the last line (overwriting text you've already written there), but you can't write to any line other than the last one.
To understand why console IO works this way, you should know that very early computers didn't have screens. Instead, their console output was directly printed out one line at a time on paper. While some line printers could print several characters on the same spot (to get effects line strikethrough or underline), you couldn't unprint anything once it was on the paper. Furthermore, the paper feed only worked in one direction. Once you had sent a newline character that told the printer to advance the paper, you couldn't go back to an old line again.
I believe this would solve your problem:
print(f">>> {input()} ------")
OR
print(f"{input(">>>")} ------")
F-strings are quite useful when it comes to printing text + variables.

Fortran: odd space-padding string behavior when opening files

I have a Fortran program which reads data from a bunch of input files. The first file contains, among other things, the names of three other files that I will read from, specified in the input file (which I redirect to stdin at execution of the program) as follows
"data/file_1.dat" "data/file2.dat" "data/file_number_3.txt"
They're separated by regular spaces and there's no trailing spaces on the line, just a line break. I read the file names like this:
character*30 fnames(3)
read *, fnames
and then I proceed to read the data, through calling on a function which takes the file name as parameter:
subroutine read_from_data_file(fname)
implicit none
character*(*) fname
open(15,file=fname)
! read some data
end subroutine read_from_data_file
! in the main program:
do i=1,3
call read_from_data_file(trim(fnames(i)))
end do
For the third file, regardless of in which order I put the file names in the input file, the padding doesn't work and Fortran tries to open a with a name like "data/file_number_3.txt ", i.e. with a bunch of trailing spaces. This creates an empty file named data/file_number_3.txt (White Space Conflict) in my folder, and as soon as I try to read from the file the program crashes with an EOF error.
I've tried adding trim() in various places, e.g. open(15,file=trim(fname)) without any success. I assume it has something to do with the fix length of character arrays in Fortran, but I thought trim() would take care of that - is that assumption incorrect?
How do I troubleshoot and fix this?
Hmmm. I wonder if there is a final character on the last line of your input file which is not whitespace, such as an EOF marker from a Linux system popping up on a Windows system or vice-versa. Try, if you are on a Linux box, dos2unix; on a Windows box try something else (I'm not sure what).
If that doesn't work, try using the intrinsic IACHAR function to examine each individual character in the misbehaving string and examine the entrails.
Like you, I expect trim to trim trailing whitespace from a string, but not all the characters which are not displayed are regarded as whitespace.
And, while I'm writing, your use of declarations such as
character*30
is obsolescent, the modern alternative is
character(len=30)
and
character(len=*)
is preferred to
character*(*)
EDIT
Have you tried both reading those names from a file and reading them from stdin ?

Strange chars at the end of a line in linux. Reading line with getline

At the begining, I'd like to say that I'm very beginer with linux and stuff.
I'm reading a file (line by line) with use of a GetLine.
At the start, I open file descriptor with function open.
Then I change (int)f_descriptor to (FILE*)f_stream (because GetLine requires FILE* arg).
I'm splitting whole line into words (space is a separator) and I place them into a char** words_array. Everything works Ok as long as it's not the last word in the line. For some reason, last words have got some strange chars at the end of them. It doesnt happen always.
Why could this happen?
By the look of things, I suspect you're not null-terminating the last string. The length it's reporting is correct, but the fact that you've got extra bytes might mean that you're copying things into an area of memory that has some, but not all, null bytes in it initially but you're not adding an explicit null byte. If you want a better answer it will help if you can post some of the code where you're reading the data.
You can't simply change an (int) f_descriptor into a (FILE*) f_stream. They're two very very different things. If you use open() to get f_descriptor, you need to use read() and write() to access the file, but if you use fopen() to get f_stream, you use fgetch(), fread(), fwrite(), gets(), puts(), fprintf(), fscanf(), etc.

strings in fortran and filenames using them

here is the trouble ... i'm dynamically building (rather changing)a string which contains numerals(numbers) (like to have filename out01.txt ,out02.txt etc ..)
my program works fine (i'm using the last updated value string to name a file and edit that file) ... but in the same directory with "ls" command, i can see that file created and through file browser i can acess it but from command line using vim , gedit i can't open it new file of same name is opening... moreover i can't remove that file from command line (rm out010.txt'
no such file or directory) here is the code , i might not have been able to explain my problem but code will speak for itself ...
program strtest
implicit none
character(len=1024)::filen,format_str
integer::i
format_str="(a5,i0.3,'.txt')"
do i=1,10
write(filen,format_str)'out',i
end do
write(*,*)trim(filen)
open(23,file=trim(filen))
write(23,*)"what a mess!"
close(23)
stop
end program strtest
note: i have the same problem even without using trim() function in file opening statement
please explain my situation!!
regards ...
Your filenames are coming out with 2 spaces in front of them, so if you put rm " out01.txt" (2 spaces,out01.txt) you will be able to delete them. It's the a5 that's throwing off the format string.
As #jonsca pointed out already, the problem is with the extra whitespace. The simplest way to get rid of it is to use adjustl, like this:
open(23,file=trim(adjustl(filen)))

Resources