Using the Free Pascal IDE, I have some problems with console IO under Windows.
When I use Writeln the cursor doesn't move to the next line and is staying at the end of the written line. So if I press Enter instead of moving to the next line the cursor goes to the beginning of the current line. And if I use Readln after Writeln new characters that I enter are displayed instead of the characters that were written with Writeln.
When I use Readln to enter string I have next problem. If I enter some string, use backspace to delete characters and enter other characters after that, Readln return to program string that includes backspace symbols and deleted characters.
I guess this is Windows problem, but I have no ideas.
It looks as if you have run into a (known) problem with the Free Pascal IDE on Windows 10. This happens to console programs started from that IDE. There is no fix, and no workaround (well, there is one: use unit Crt). It has other issues as well.
Just try another IDE, e.g. Lazarus.
Note that your program will run fine if started standalone. But not when started from the FPC IDE.
Related
I am working my way through Learn You a Haskell for great good. I am currently on the files and streams section of Chapter 9. For some reason, when I try to pipe code into one of the example Haskell programs, I do not get the same output as the book. Using ConEmu for Linux commands on Windows. For example, I have the program that only prints out strings that are less than 10 characters with the code below (short_lines.hs):
main = interact $ unlines . filter ((<10) . length) . lines
I am going to be passing this file (short_long.txt):
i'm short
so am i
i am a loooooooooong line!!!
yeah i'm long so what hahahaha!!!!!!
short line
loooooooooooooooooooooooooooong
short
Here is the command:
cat short_long.txt | runhaskell short_lines.hs
Here is my output:
so am i
short
The book says that the output is the following:
i'm short
so am i
short
I believe this has to do with the handling of the newline character but I can't figure this out since lines should have removed the newline characters before filtering. It works with manual input but not with piping. Why am I getting a different output? Am I doing something wrong? I tried removing trailing newline characters in Atom editor but it didn't change anything. Any help on why I am not getting the expected result and what I could do to get the expected result would be greatly appreciated. Thank you!
The default newline mode for stdin is nativeNewline, which chooses its behavior based on what it believes your OS to be. I suspect that it has (wrongly) decided you are on a Unix system and it therefore should not do CRLF conversion; thus when given a Windows-style file each line has a trailing '\r' character. Try using
import System.IO
main = do
hSetNewlineMode stdin universalNewlineMode
interact $ unlines . filter ((<10) . length) . lines
to force CRLF conversion and see if that gets you the expected results.
I can reproduce your problem on my Unix system by converting a text file to DOS mode before giving it to your program. Having done so, my suggested fix gets the desired behavior.
I found out that I can change the line ending style from Windows-CRLF to Unix-LF on Atom editor. Currently it is located on the bottom and simply says CRLF or LF. You can click on it to choose a different line style. For this book, that is what I will use for simplicity's sake. However, I believe that amalloy's answer is a better long-term universal approach to IO.
For some reason, Haskell on my machine does never return from any getLine call. For instance, I tried to run the following code straight from Learn You a Haskell for Great Good:
main = do
putStrLn "Hello, what's your name?"
name <- getLine
putStrLn ("Hey " ++ name ++ ", you rock!")
When I run it, the first line is printed, and I see my input when I type a name, however when I press Enter the program just blocks there and never prints the final line.
How should I fix this?
edit: I am running it from the Sublime IDE, maybe that has something to do with it
After doing a quick search on how Sublime runs programs, I found a youtube video (edit: and this SO post) which says that Sublime's "run program" functionality can only show output and isn't capable of reading input.
So it looks like you'll have to run your program from the command line or from within GHCi using :main. The latter might be the most convenient as Sublime actually supports a GHCi tab, so you can still do everything from within Sublime.
This seems to be a limitation in Sublime's Build command (assuming that this is what you're using).
Sublime executes the script using runhaskell, but apparently, it doesn't capture STDIN (which makes kind of sense - build results are usually read-only and not an interactive session).
Workaround: run your script from the command line with
runhaskell script.hs
and everything works as expected
This is a most strange problem, which I only get it using GNU Screen and a Nokia N900. Under vi (both vim and nvi, it turns out) if I type in insert mode one<Enter>two I get
Mtwo
one
So, not only does <Enter> put an M at the beginning of the line, but actually it does something pretty weird in the meantime. Among other things, this issue doesn't let me save and exit.
Any thoughts? Thanks in advance.
POSSIBLE ANSWER: It turns out I wasn't the only one having this issue, which is gone (at least in our case) by simply adding term xterm to your .screenrc. Sorry for not doing a more thorough search before asking...
Your <Enter> is like a carriage return and linefeed (<CRLF>). That's ASCII 10, followed by ASCII 13 (which is the CTRL-M you see). The screen, however, isn't supporting it. Try to do:
export TERM=vt100
then run vi, etc or just
TERM=vt100 vi
which should fix the behavior (assuming you have vt100 terminal capabilities).
In C++ I'm trying to go back up a line to add some characters.
Here is my code so far:
cout << "\n\n\n\n\n\n\n\n\n\n\xc9\xbb\n\xc8\xbc"<<flush;
Sleep(50);
As you can see, I have 10 newline characters. In my animation, a new block will be falling from the top of the screen. But I don't know how to go back up those lines to add the characters I need. I tried \r, but that dosen't do anything and \b dosen't go up the previous line either. Also, what exactly does flush do? I've only been programming in C++ for about 2 days so I'm a newb =P.
Thanks so much!!!
Christian
If your console supports VT100 escape sequences (most do), then you can use ESC [ A, like this:
cout << "\x1b[A";
to move the cursor up one line. Repeat as necessary.
In windows you can use this example
there you will create CreateConsoleScreenBuffer()
and then are using SetConsoleCursorPosition(console_handle, dwPosition);
cout will first write to internal buffer and only output it to the screen periodically and not for every character that gets inserted. This is for performance reasons.
flush tells it to empty the buffer now and show it on screen.
You should consider a library like ncurses.
My program allows the user to specify a file which is read as input, however this is optional. If the user does not specify a file, I'd like to read input in from the command line.
I have this so far:
main :: IO()
main = do
(opts, mbArgs) <- parseCmdLine
input <-
case mbArgs of
Nothing -> getContents
Just file -> readFile file
This doesn't seem to be working. When a user doesn't stipulate a file, they are able to enter input, but there seems to be no way of terminating so that the program can then work on that input.
I thought that you had to press Ctrl+D, but that doesn't do anything.
Thanks for any help.
In a typical Unix-like terminal (such as Cygwin, at least in Cygwin's rxvt; not sure about the Windows Command Prompt), a Ctrl+D only sends EOF when you're at the start of a line. If you hit Enter and then Ctrl+D, it should work. If you want to send EOF without a newline, hit Ctrl+D twice in a row.
If it's not that, then there's presumably some other problem with your terminal; the code looks fine.
I'm going to write hammar's comment as an answer.
For me on windows, hitting ctrl+Z crtl+Z enter worked.