How do I write text to the console without a newline in Fortran? - io

I'm writing an interpreter for Brainfuck (https://en.wikipedia.org/wiki/Brainfuck) in Fortran and I need a way to write a character to the console without also adding a newline. Because it's Brainfuck, I can't assemble the characters into a string that is printed after the program has finished.
I have tried something along the lines of:
WRITE(*, "(A)") characterToPrint
But the console only shows the first character that is printed (e.g. when trying to interpret a Hello World program, a single "H" would be visible and nothing else). Interestingly, though, the following works just fine:
WRITE (*, "(A)", advance = "no") 'H'
WRITE (*, "(A)", advance = "no") 'e'
WRITE (*, "(A)", advance = "no") 'l'
...
Is there something about WRITE that would cause this that I'm not aware of? What else could be causing this issue?

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.

Lisp weird new line before print

When I'm trying to print some text from terminal using "clisp hello.lisp" (where "hello.lisp" is name of my program) then new line is added before right output (so before "hello world"). Why it happens and how can I change this? In other languages like Python or Ruby there is no newline in the same code.
(print "hello world")
Why it happens
It happens because the function PRINT is specified to do that.
and how can I change this?
You can't. Just use a different function for printing: WRITE-STRING, WRITE-LINE, WRITE or PRIN1.
WRITE-LINE prints the string and then prints a newline.
In other languages like Python or Ruby there is no newline in the same code.
Since these are different programming languages, the same code will not run in Python and Ruby.
Use prin1 instead of print:
(prin1 "Hello world")
There's also a function named prin1. This is same as print except it
doesn't add a newline.

Multiline input prompt indentation interfering with output indentation

I have a function that prints the number of pixels found in an image and then asks the user how they would like to proceed. As long as the interpreter hasn't moved on from the function I want all the output to be indented accordingly.
One such 'sub output' (the input prompt) needs to be multiple lines. So I kick off with the 3*quote (''') followed by two spaces to create the indentation. At the end of the question 'how would you like to proceed?' I use a hard return. An extra indentation is assumed by the text editor so I remove it causing the following list of suggestions to line up flush with the input variable command. Here's how it looks:
def returnColors():
#
# lots of code that does stuff...
#
print("The source image contains", lSize, "px.")
print("")
command=input(''' What would you like to do? You can say:
get all
get unique
''')
The problem with this is that the interpreter is acknowledging the indentation that separates the function body from the function statement as actual string contents, causing the output to look like this:
The source image contains 512 px.
What would you like to do? You can say...
get all
get unique
|
The only way to avoid this is by breaking indentation in the interpreter. Although I know it works, it doesn't look very good. So what options do I have?
EDIT: Just because I have the screenshot_
One thing that you should keep in mind is that once you have start a multiline string declaration, all the text until it is closed is taken as is and syntax (ie, indentation) is no longer considered.
You can start your multiline with an explicit new line so that everything in the multiline string can be indented together in code.
IE.
command=input('''
What would you like to do? You can say:
get all
get unique
''')
would print out the prompt with a new line on top, but the formatting of the text is more explicitly shown and should appear as seen.
OR you could use the \n for each new line in the string to get it formatted more correctly and remember to use a single \ after each new line. E.g.
instead of:
''' What would you like to do? You can say:
get all
get unique
'''
Try
' What would you like to do? You can say:\
\n\
\n get all\
\n get unique\
\n'
The indent won't matter, no matter where you use \n at the beginning of new line, the input() will output the same. This is will give the same input() string:
' What would you like to do? You can say:\
\n\
\n get all\
\n get unique\
\n'

Using variables to open output files in Fortran

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

Resources