CLion - Carriage return? \r - c

I am using the CLion IDE and I am trying to do a carriage return.
I am doing a print statement in C and have the following syntax:
printf("\rHello World!"); which is inside a loop. The loop still prints every Hello World on its own line. There are no \n's in my program. I've tried changing the line separators options to unix mac OS and windows and none of them change the functionality. Google has also led me to no useful answers.
int main()
{
int i = 0;
while (i < 5000000)
{
printf("\rThis is line number %d!", i++);
}
return 0;
}
My expected output is only a single line of text in the console window.
Thanks.

Your problem is PuTTY console, that is used in CLion by default.
You can switch it off in Registry:
Help | Find Action | Registry... =>
run.processes.with.pty [ ] <- uncheck
I recommend you modify the program:
#include <iostream>
int main() {
int i = 0;
while (i < 500) {
printf("\rThis is line number %d!", i++);
fflush(stdout); // <- add this call
}
return 0;
}

Related

C with ncurses does not print UTF8 characters, instead prints two characters in the wrong encoding

I want to write UTF-8 Chars onto the terminal, but despite of trying everything I found here it doesn't work.
I build my program with the command "cc main.c -lncursesw" and I include ncurses/curses.h, wchar.h and locale.h.
As described in here (How to make ncurses display UTF-8 chars correctly in C?) I do the set locale thing. I cannot access any function to print wide character and doing it prints two undesired ASCII chars. The (I think) relevant code:
#define unsetcolor(color) attroff(COLOR_PAIR(color))
#define to_full_color(f, b) (b << 3) | f | 64
#define setcolor(color) attron(COLOR_PAIR(color))
void init_colorpairs()
{
for(int f = 0; f < 8; f++)
{
for(int b = 0; b < 8; b++)
{
//I just do color things
int color_pair = to_full_color(f, b);
init_pair(color_pair, f, b);
}
}
}
int main()
{
setlocale(LC_ALL, "");
WINDOW* main_win = initscr();
if(has_colors() == FALSE)
{
return -1;
}
start_color();
init_colorpairs();
//window size x, y
int win_s_x, win_s_y;
getmaxyx(main_win, win_s_y, win_s_x);
//curs_set(0);
//0=succsess, -1=error
attron(A_BOLD);
for(int i = 0; i < (win_s_x * win_s_y); i++)
{
u8 color = (i&63)|64;
setcolor(color);
//My try to print an UTF-8 char, i also searched for addchw or mvaddv
addch(9617);
unsetcolor(color);
}
refresh();
attroff(A_BOLD);
//refresh();
getch();
endwin();
return 0;
}
I found out what went wrong.
Event though ncurses is a C library, it has only very limited unicode support. The only (at least for me) working functions with the desired symbols are printw and addch_wch (printing and compilation succsess, not really what I needed but they worked). For more functions like mvaddch_wch you should use ncursesw (the ncursesw/ncurses.h header). This is an C++ header, so the solution is to compile with a C++ compiler or write some sort of wrapper.
Further more I want to add how I used the for me needed function, in case someone is too new to ncurses and has the same problems.
extern "C"
{
#include<ncursesw/ncurses.h>
}
/*somewhere in the main function*/
//set the character and it's attribute, the char value can be representet as an integer number
cchar_t c = { A_BOLD | COLOR_PAIR(1), 9617};
//the char is given as a reference, and the coordinates as values
mvadd_wch(1, 10, &c);

Print utf8 wchar_t in windows console

I'm running CodeBlocks in Windows 10 and even after following some of the indications found on SO, I did not manage to print in cosole utf8 chars.
_setmode(_fileno(stdout), 0x00020000); // _O_U16TEXT
setlocale(LC_ALL, "C.UTF-8");
wchar_t star = 0x2605;
wchar_t st = L'★';
if (printf("%lc\n", star) < 0) {
perror("printf");
}
if (printf("\n%lc\n", st) < 0) {
perror("printf");
}
gives the output
printf: Invalid argument
printf: Invalid argument
The console Properties >> Font is set to Lucida Console, similar to codeblocks code editor. I am able to see the star as a symbol inside my text editor but not in the console. What should I do?
EDIT:
even if I change the previous code to:
if (wprintf(L"%lc", star) < 0) {
perror("printf");
}
printf("\n");
if (wprintf(L"%lc", st) < 0) {
perror("printf");
}
I am not able to see more than two rectangles instead of the stars (which if I copy and try to paste them here, are drawn in the right way)
After trying all of my available fonts in the console, the only one which let me see the actual star is SimSun-ExtB.

Xcode Seemingly Unable to Use Arrays in C

It's very likely that this is user error as I'm somewhat new to Xcode, but I can't figure this out. Hopefully one of you can.
I'm trying to add an array using the following code:
int ArraySum (int MyArray [], int size) {
int sum = 0;
for (int i = 0; i< size; i++) {
sum = sum + MyArray [i];
}
return sum;
}
int main (){
int mynumberarray [6] = {1,2,3,4,5,6};
int the_sum = ArraySum (mynumberarray, 6);
printf ("The Sum is = %d \n", the_sum);
return 0;
}
When I click the build & run button in Xcode, the only output I get is (lldb).
This would typically lead me to believe that I made a mistake somewhere, but when I run the code through Terminal it runs perfectly and gives me the correct sum.
Can anyone help me here? This isn't an isolated incident, I've had several issues working with arrays in Xcode that are working perfectly when executed in Terminal.
Screenshot:
You have breakpoints set in your Xcode project, so the program is pausing at the first breakpoint and giving you the (lldb) debugger prompt. Disable the breakpoints and run and everything should behave as expected. You can either manually disable each breakpoint (by clicking on it - it should then become dimmed), or go to Debug => Disable Breakpoints in the menu bar.

Prevent output of carriage return with readline

I'm new to the Gnu Readline library.
I need to call the readline() function when the cursor is at the very last line of the console. But I need to prevent scrolling down when the Return key is pressed; so I'm looking for a way to prevent the output of the carriage return : I'm sure it's possible, but can't find the way to do it.
I tried to use my own rl_getc_function() to trap the Return key (the example below traps y and z keys, but it's just for test purposes) and treat this key in a special way:
My first idea was to run the accept-line command directly, thinking it would not output a carriage return, but actually, it does
My second idea was to redirect the output to /dev/null before calling the accept-line command; but the redirection doesn't seem to apply when the readline() function is already running.
Here is an example of my tests:
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
FILE *devnull; // To test output redirecting
int my_getc(FILE *file)
{
int c = getc(file);
// Let's test something when the 'y' key is pressed:
if (c == 'y') {
// I was thinking that calling "accept-line" directly
// would prevent the output of a carriage return:
rl_command_func_t *accept_func = rl_named_function("accept-line");
accept_func(1, 0);
return 0;
}
// Another test, when 'z' key is pressed:
if (c == 'z') {
// Try a redirection:
rl_outstream = devnull;
// As the redirection didn't work unless I set it before
// the readline() call, I tried to add this call,
// but it doesn't initialize the output stream:
rl_initialize();
return 'z';
}
return c;
}
int main()
{
devnull = fopen("/dev/null", "w");
// Using my function to handle key input:
rl_getc_function = my_getc;
// Redirection works if I uncomment the following line:
// rl_outstream = devnull;
readline("> "); // No freeing for this simplified example
printf("How is it possible to remove the carriage return before this line?\n");
return 0;
}
I'm sure I missed the right way to do it; any help would be appreciated.
I found it : the rl_done variable is made for this.
If I add this code to my my_getc() function, it works well:
if (c == '\r') {
rl_done = 1;
return 0;
}
No carriage return is then inserted, and my next printf() call displays just after the last char I typed.

Why does Sublime Text report that my program terminates with exit code 1?

I was using Sublime Text to compile a code I made, but it gave me this error:
[Decode error - output not utf-8]
[Finished in 0.2s with exit code 1]
I though it was because I didn't save it to UTF-8, but when I save with UTF-8 it gives me the same error. I tried to compile it using IdeOne, it compiles fine, but it gives Time limit exceeded, but this is because of the code. Here is my code:
#include <stdio.h>
int main(void) {
int x, d;
for(x=1; ; x++){
for(d = 2; d <= 20; d++){
if(x%d != 0){
break;
}
}
if(d == 21){
break;
}
}
printf("%d", x);
return 0;
}
It may seem it has an infinite loop but it didn't.
Your program is fine but it takes a while (2.3s on my computer, yours might be slower) to terminate which is the reason why IdeOne complains. The first x to satisfy your test is 232792560.
See:
https://gist.github.com/clijiac/2814161
SASS on Sublime Text 3 - [Decode error - output not utf-8]
Sublime Text 3, Python 3 and UTF-8 don't like each other
https://www.sublimetext.com/forum/viewtopic.php?f=3&t=7809
A possibility is that it might be missing the directory of the compiler in the PATH variable. So, to do that, go to:
Control Panel > System and Security > System > Advanced system settings
Click on "Ambient Variables" and add the path on the system variable "PATH".

Resources