sending EOF to stdin in Clion IDE - c

Is there a way to send EOF to stdin when running a c program in Clion IDE?
I tried ctrl+D - it simply shuts the program down.
Ctrl+Z doesn't work either.
Any ideas?
BTW - I'm using it on a Windows 10 OS.

ctrl+D does the job, however there is a known problem, to avoid it disable option run.processes.with.pty in Registry (Find Action > Registry).

Type in ^ on a newline, in the console after your input, I found it does the trick.

Another option is to run the program from command line, and attach to it from CLion.
In that case, you'll have your native shell.
It's detailed at
https://blog.jetbrains.com/clion/2016/01/attach-to-local-process/
The process is very simple - after running the program from command line, choose Run->Attach to Local Process...
And choose your program from the list. You can type in the search field.

Related

How do I make a C executable for Windows written on Linux not close the cmd as soon as it finishes running?

So, I've written a C executable on Linux for Windows using mingw32-gcc. It is a basic Input-Output program, you type an input and get an answer.
Problem is the cmd shuts down immediately, so the user can't see the output.
Assuming I cannot use Windows to edit the executable there, what should I change in my code/ what flags should I use when compiling it?
inb4:
the user is supposed to click and run it, so running it from cmd won't help.
adding getchar()/scanf() at the end of my code doesn't work, and it feels a bit like cheating.
SOLVED: so all I had to do was to actually add a getchar() after every scanf() and one more at the end of the code for the user input to close the cmd.
Waiting for input at the end is not cheating, but common practice. How else should program know for how long it should stay opened? Closing program by closing console window directly is more cheating than waiting for user input to finish.
You can for example prompt user to hit any key like Press any key to exit... or something similar.
If you want some specific delay, you can use Sleep() from windows.h instead of waiting for input.
See https://stackoverflow.com/a/3379146/3035795

open vim from C system command

I am noobie with C.
I've seen system() and fork() and execv() stuff, but I do not think they are what I need ( or they just do not work )
I just want to my C program to open vim immediately of a file path to edit, then i can quit vim and the program continues, in my case it will just exit.
Think like git commit, it opens vim for me then when I save+exit it sends the commit.
Any ideas?
System(3) will do the work, but beware that if you call your program with the input/output redirected not to a terminal, this will propagate to the execution of vim and it will not work.
vim(3) requires that the input and output are directed to a terminal line, it cannot work on redirected input, so the best way to call it should be (with system()):
system("/usr/bin/vim file </dev/tty >/dev/tty");
The redirection clauses specify that the input and the output be redirected to the session controlling terminal, so you'll get it working even if you have redirected standard input/output in the main program.
system() is what I was looking for, I was using it incorrectly, but it's simple just use:
system("vim path/to/file.txt");
will open vim of current directy and work like expected ( like git commit )

Why does the terminal show "^[[A" "^[[B" "^[[C" "^[[D" when pressing the arrow keys in Ubuntu?

I've written a tiny program in Ansi C on Windows first, and I compiled it on Ubuntu with the built-in GCC now.
The program is simple:
read the line from console with scanf().
Analyze the string and calculate.
But something weird happens. When I try to move the cursor, it prints four characters:
pressing Up prints "^[[A"
pressing Dn prints "^[[B"
pressing Rt prints "^[[C"
pressing Lt prints "^[[D"
How can this be avoided?
Why does it print these 4 characters instead of moving the cursor?
Because that's what the keyboard actually sends to the PC (more precisely, what the terminal prints for what it actually receives from the keyboard). bash for example gets those values, deciphers them and understands that you want to move around, so it will either move the cursor (in case of left/right) or use its history to fetch previous commands (up/down). So you can't expect your program to magically support arrow keys.
However, reading from standard input from the terminal already supports left/right arrow keys (I believe, but I'm not in Linux right now to test and make sure). So my guess is that there is another issue interfering. One possible cause could be that one of your modifier keys is stuck? Perhaps ALT, CTRL or SUPER?
For those who are coming from the osx (mac) try changing the shells to bash
Terminal -> Preferences -> Shells open with -> [select] Command (complete path)
then paste
/bin/bash
This might be because the user account is created in shell. You can change it to bash by two ways.
Permament solution is -
sudo chsh -s /bin/bash ${username}
To get this solution working you will have to logout and login
Temporary solution is everytime when you login into the ubuntu server type bash and hit return.
If it's under a docker container, run /bin/bash . This helped me solve the problem.
Additionally to what Shahbaz mentioned, I realized that pressing enter (thus sending an empty command) can fix the problem. This is usually necessary after using CTRLC to cancel a command.
On MacOS Terminal for me was enough to uncheck "Scroll alternate screen" for the issue to disappear. See screenshot of the preferences below.
You can (re)bind keys. Add this at the bottom of your .profile, .zshrc or whatever shell config you have.
bindkey -e
bindkey '\e\e[C' forward-word
bindkey '\e\e[D' backward-word
i think simple way is we can just do
kill %%
because this sometimes happen because of background processes.

How do you keep the console from closing after the program is done in C? [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
What is the Best Practice for Combating the Console Closing Issue?
How do you keep the console from closing after the program is done in C? When I try to search for it I find lots of stuff about C++ and other languages, but nothing for C. Also, even for C++ there doesn't seem to be a definitive answer.
So could someone please let me know what the simplest way (doesn't need to be super elegant) way to keep the console open after a C program is done running?
The previous answers are all assuming that you want to invoke the console app and then essentially leave it "running" and waiting for user input to terminate. If this is the correct assumption, then +1 to GMan's answer. However, if you are asking how to invoke this console app from either a shortcut, Start->Run or some other mechanism and leave the cmd window open, then you will need to invoke it via cmd.exe itself with the /k option like so:
cmd.exe /k "foo.exe"
This will start a cmd window, run your console app, and then leave the cmd window open. This will address #Thanatos above. He's correct in that you should let the console app close. Again, for me it's unclear what you're really asking for what the end goal should be.
If I made the wrong assumption(s), feel free to -1 me.
run the program from the command
line, instead of executing it
directly.
Ctrl+F5 in Visual C++.
Console applications are meant to be run from the console. If you do that, after running you'll be left with your console window, and can easily view the output of your program.
You can use something like getchar() to force the application to wait for a key-press.
Let the console close.
If you prohibit, in the program, the console from closing, it will make automation with your program difficult, or it will make the format of the program's input strange.
Instead, fix whatever's running the program in the first place, to not close the terminal window in the first place. If this is MS Visual Studio, try F5 (Start without debugging). If you need debugging, place a breakmark at the program's end. Otherwise, open a command prompt/terminal and run the program there yourself.
1) Your IDE opens the console before the program begins.
2) your program ends
3) the IDE closes the console
a) Just tell the IDE to not close the console ... or
b) make your program not end.
a) No idea how to do it.
b) right before the return 0; used to terminate the program add
printf("Press ENTER a few times to terminate the program");
fflush(stdout);
getchar(); getchar(); getchar(); getchar();
getchar(); getchar(); getchar(); getchar();
return 0;
You could use getch() at the end of your program.
Another way is to debug the program and place a break point before the end of the program.

ftp client controlled by pipe in C

I am trying to control ftp client from C program (OS X). I did fork and execve - process is started ok. The problem is with pipes - I can send command to ftp client process and get feedback from it just fine (If i send "help\n" i get back help output) but what I never get in pipe is "ftp> " prompt. Any ideas?
Ivan
Your ftp client is probably behaving differently if stdin/stdout is a terminal or something else (lots of program do, for a start the C library does buffering in a different way...) If you want to control that, search information about pseudo-terminals, that's a little too technical to be explained here. (And looks first at programs like expect, it's possible you won't have to write yours).
A program can examine stdin to find out whether it's a terminal or a pipe. In your case, the FTP program probably does that (for example to know whether it can use escape sequences to render progress bars or offer command line editing).
If you really need the prompt, you have to look into PTYs (pseudo terminals) which emulate a console.
wild guess: isn't the "ftp>" prompt written to STDERR ?

Resources