I found whenever I put input using scanf, and then pressing enter key to put the value into variable, it automatically makes new line.
For example,
write any float : 12.34
here is your type : 12.34
I don't want above but I want it to be like below,
write any float : 12.34 here is your type : 12.34
How can I do this? if there is no way to do this using scanf, please let me know alternative.
Thank you very much.
You cannot in a portable way.
The rationale behind is that keyboard input is normally echoed and line oriented to allow backspace processing at low level and only present a validated input to the program. The counterpart is that input is only available when the new line key (Enter) is hit, and that (because of the echo) automatically causes the cursor to go at the beginning of a new line.
What are the possibilities:
use a full GUI program. You have input fields on a window and the validation is normally done by clicking a button
use a screen positionning library. curses is a well known library in the Unix-Linux world, but ports exists for the Windows world. That allows to position the cursor at an absolute position on the screen and as such to move it immediately after the previous input.
use raw input - what curses does under the hood. All incoming characters directly hit the program and no automatic echo occurs. You must be prepared to echo each received character (the way you want, if you want to automatically upper- or lowercase input, you can), including backspace. That's really low level processing, and unfortunately it works differently in Windows (search for conio functions) and Unix-Linux (search for raw vs cooked input)
accept it. Going to the next line after each and every input is anyway common and should not surprise user.
Related
I'm very new to coding in general so please excuse any stupid things I say.
I am trying to print (in c) a rather long text using printf() but since it can't all fit on the screen it jumps to the end of the text and the beginning is not visible unless you scroll up. Is there an easy way to have it print the long text but stay at the beginning and allow the user to scroll down as they read before they put in the next command?
On Unix (including Linux and Mac), there are command line programs built in called more and less that does exactly what you describe. more is a program that simply waits for the user to press enter or space before showing the next page of output. less is slightly improved in that it allows vi editor keystrokes (such as j and k) to scroll up and down in the output.
more is also available on the Windows command line as well. You might even be able to find a version of less for Windows as well.
c:\users\selbie> your_program.exe | more
$> ./your_program | less
As to how to do this programmatically, that's a bit more difficult as it would involve measuring the console width and implementing your own scroll buffers. There might be open-source libraries that provide this functionality, but the console environment already has a solution for apps that produce long output.
Not really, though you may find a reasonable and simple solution is to print only a certain number of lines (say 30), then prompt the user to press Enter before display more lines.
You can even find out the current size of the terminal. That's platform specific; for Linux it's explained here: How to get terminal window width?
Not in a standard way, no.
Your output stream in C is just a stream of characters -- scrolling is handled by your terminal.
Depending on your terminal, it may be possible to control scrolling by outputting special characters, like ANSI escape codes. The ncurses library provides a portable way to manipulate terminals.
However, if you just want a more convenient way to look through your output (or really any command output), #selbie's answer is the best: use more or less. This will avoid any extra complexity in your program.
I don't really know how to explain this so the title is probably misleading.
I'm making a small text editor in C and I'm saving the contents of an entire file into an array of chars. Now I want to display the entire string to the user in a way that the user will be able to edit the string inside the command window by positioning the cursor/seek and then typing or deleting characters. Pressing enter or some other key would save the text to a new string and write it to the file.
Similar to what happens when you open a file in a linux text editor like nano or vi... You get that cursor which you can move around the file and make changes.
How can that be done in C? I don't need it to be like in the linux editors, simply putting the string inside the input area (as if the user typed the text) would be enough.
You must use Terminal capabilities (a.k.a Termcaps). They are special characters which can be interpreted by your terminal (e.g moving the cursor back and forth, or clearing the screen). Here's what Wikipedia says about Termcap databases :
A termcap database can describe the capabilities of hundreds of
different display terminals. This allows programs to have
character-based display output, independent of the type of terminal.
On-screen text editors such as vi and emacs are examples of programs
that may use termcap.
By using terminal capabilities, you will be able to control the way the cursor behaves in your editor, and how input characters shows up. A good example of this would be using colors for syntax highlighting.
I would suggest that you use the GNU Termcap library to build a cross-terminal application. Once you get the main principles, its usage is relatively straightforward, I used it back in the days to actually build a shell (such as bash, or sh).
Next, you might also want to look at the differences between canonical and non-canonical terminal modes. Given what kind of functionalities you are wanting to implement in your editor, you will likely want to override the way your terminal interprets some inputs.
See also :
Moving the cursor using Termcaps
Characters for Input Editing only available in canonical mode
This is done by writing certain special control characters to stdout, which can do things like set the color, move the cursor, etc. See https://en.wikipedia.org/wiki/ANSI_escape_code for more info.
However, if you want your editor to be portable, or you don't want to worry yourself with details, you may want to consider using the ncurses library (https://en.wikipedia.org/wiki/Ncurses), which editors like nano/vi use rather than doing it themselves.
I am currently using libedit for "readline"-functionallity, so far it works really well, but I would like to display characters (spaces) after the prompt which are deleteable by the user. Similiar to IPython:
As you can see there are 4 additional characters (spaces) automatically added, which I can remove by pressing backspace, until I reach the prompt ...:.
I would like to mimic this behaviour with libedit/editline and if there is no way to do this (e.g. using curses or the underlaying terminal function which libedit uses), I would switch to readline.
How is this possible to do, preferable with libedit/editline or readline.
Edit: I tried modifying rl_line_buffer, this didn't modify the display, only the value I get from readline.
A lot of times I've seen text-based programs which replace text which has already been printed. For instance, imagine a program where the progress is printed as
Loading: 5%
and then it says
Loading: 10%
and so on, without printing new text which is appended?
How is this done? I haven't seen any such functions available in the library (in this case C). I have an idea, though: There is a character you can write which returns the prompt to the beginning of current line (\r I believe). Could this be used to "overwrite" what you've already printed to the command prompt?
In most consoles, writing a bare carriage return \r without a newline after it will return the cursor to the beginning of the current line, allowing you to overwrite the existing text. Writing the backspace character \b also moves the cursor back one character.
For simple behavior, such as a progress indicator, this is all you need. For more complex behavior, you need to control the terminal through non-standard means. On Unix-based systems, the ncurses library can be used—it gives you full control over the cursor location, text color, keyboard echoing, more fine-grained keyboard input, and more.
On Windows, there's a suite of functions for manipulating consoles, and they can do mostly the same things as Unix consoles.
One way that I have seen is to just print the backspace character a number of times and then replace whatever you erased with the new text.
The backspace character is an ASCII control character represented by \b.
I'm developing a CLI program, in C, for my systems class project, and it needs to display incoming text while maintaining a command prompt. Left alone, the incoming text will saw through whatever one tries to type. In other applications I've seen the incoming text print above(or below) the prompt itself. Is there any way to implement this in ANSI escapes? ncurses seems like overkill.
You can print \r to erase the prompt: It will return the cursor to the beginning of the current line. You can then print your output followed by some spaces to clear out any remaining input characters, newline, and reprint the prompt.
With ANSI sequences or terminal-specific libraries you can do even more, but this I think is all you can do reliably using only ASCII. Apart from printing 242 blank lines to redraw the whole screen, of course.
Edit: Sorry, I didn't answer the ANSI part properly. With cursor movement control codes and printing space over existing characters, you can pretty much do anything, and there are some convenience actions to help you, such as "delete line". But keep in mind that Windows doesn't play nice w/ ANSI post XP, and neither are other systems guaranteed to.
For one thing, if you want to maintain a prompt, while printing, you can not use things like scanf. You have to intercept keyboard events or use a non waiting method to get input. Then you can get the terminal number of lines (n) and print the last n-1 lines of your output, and then a prompt.
my2c