I've wrote this very basic code-lines with colored output:
printf("\033[1;32m"); // boldgreen code: \033[1;32m according to:
http://web.theurbanpenguin.com/adding-color-to-your-output-from-c/
puts("Enter username:");
gets(user);
In my computer evreything works fine and I get colored output as expected:
but in other computers I get this Output:
\033[1;32mEnter username:
I have to say that all my #includes are fine, Im just doing copy-paste to another computer & if thats important Im using Visual Studio in both Computers.
seems like basic thing but I don't understand why thats happend.
Thanks for helpers.
Most terminals support colors. The problem is sending the right escape code. For the Windows command line, you have do a different escape sequence. There is a wikipedia entry that describes how to do an escape in different environments.
Related
I'm new to C and I came across this code and it was confusing me:
sprintf(banner1, "\e[37╔═╗\e[37┌─┐\e[37┌┐┌\e[37┌─┐\e[37┌─┐\e[37┌─┐\e[37┌─┐\e[37m\r\n");
sprintf(banner2, "\e[37╠═╝\e[37├─┤\e[37│││\e[37│ ┬\e[37├─┤\e[37├┤\e[37 ├─┤\e[37m\r\n");
sprintf(banner3, "\e[37╩ \e[37┴ ┴┘\e[37└┘\e[37└─┘\e[37┴ ┴\e[37└─┘\e[37┴ ┴\e[37m\r\n");
I was just confused as I don't know what do \e[37 and \r\n mean. And can I change the colors?
This looks like an attempt to use ANSI terminal color escapes and Unicode box drawing characters to write the word "PANGAEA" in a large, stylized, colorful manner. I'm guessing it's part of a retro-style BBS or MUD system, intended to be interacted with over telnet or ssh. It doesn't work, because whoever wrote it made a bunch of mistakes. Here's a corrected, self-contained program:
#include <stdio.h>
int main(void)
{
printf("\e[31m╔═╗\e[32m┌─┐ \e[33m┌┐┌\e[34m┌─┐\e[35m┌─┐\e[36m┌─┐\e[37m┌─┐\e[0m\n");
printf("\e[31m╠═╝\e[32m├─┤ \e[33m│││\e[34m│ ┬\e[35m├─┤\e[36m├┤ \e[37m├─┤\e[0m\n");
printf("\e[31m╩ \e[32m┴ ┴┘\e[33m┘└┘\e[34m└─┘\e[35m┴ ┴\e[36m└─┘\e[37m┴ ┴\e[0m\n");
return 0;
}
The mistakes were: using \r\n instead of plain \n, leaving out the m at the end of each and every escape sequence, and a number of typos in the actual letters (missing spaces and the like).
I deliberately changed sprintf(bannerN, ... to printf to make it a self-contained program instead of a fragment of a larger system, and changed the actual color codes used for each letter to make it a more interesting demo. When I run this program on my computer I get this output:
The program will only work on your computer if your terminal emulator supports both ANSI color escapes and printing UTF-8 with no special ceremony. Most Unix-style operating systems nowadays support both by default; I don't know about Windows.
I want to display output on different screen, but how?
For example:
I want to display " hello world " on the first black screen, and when I press enter button, the old screen will disappear and another new black screen will display "thank you" .
Just how to do that in c?
Depending on what you want but just clear the screen after getting an enter.
puts("\033[2J"); will work on most terminal using VT100 escape codes.
clrscr() is a nonstandard function defined in <conio.h> that some older compilers use.
system("cls"); works with Windows and system("clear"); with Linux and is in <stdlib.h>.
These options would cover most environments that you would encounter when using C.
This is platform-dependent.
In Unix you have to call the corresponding APIs, Qt or GTK, to create a new terminal console.
In Windows you have to call WinAPI such as CreateConsole and such.
It would be easier if you only want to clear your current console. Using ASCII CSI escape sequence works on most Unix & Linux systems, just
printf("\x1B[2J");
Or in Windows, call
system("cls");
There's actually a more native approach which calls FillConsoleOutputCharacter and FillConsoleOutputAttribute, which introduces extra code that I'm not talking about here.
Is there anyway? I cant seem to find any function that does this. I tried googling this but could not find anything.
I do not believe it is possible. The terminal is not for such things. It is meant for displaying text in sometimes varying colors. If you want to change the font size, you would need to open a window and draw to it (That might not actually be the correct term. Graphics aren't my forte). If this is what you want to do, I suggest looking into sdl. It is fairly simple to set up and is easy (IMO) to use. And because I know stackoverflow doesn't like flamewars, I am by no means saying it is the best. Im sure there are plenty of alternatives that are just as good. I just have not used them
If the text is being displayed in an xterm then it can be changed.
Edit .Xresources and add (probably at the top):
xterm*font: *-fixed-*-*-*-20-*
xterm*geometry: 80x24+5+5
This specifies a font size of 20 with a 80x24 charactor xterminal window located in the upper left of the display (widthxheight+xoffset+yoffset)
I was unable to use a font larger than 20. I start the application from a terminal window with the command line below on Ubuntu 14.04 with XFCE GUI.
xterm -e my_c_app
Well, it can be done if you are running on Windows command windows. I did that.
Ncurses will use your console windows settings: colors, size and typeface.
This works, but it is a somewhat basic mode of use. If needed you can even change the character attributes by program, before initializing ncurses terminal.
I tried with monospaced typefaces without any problems, tried even with proportional typefaces. It works also, but here visual quality is lesser due to the "entangling" of some characters, because ncurses keeps spacing in monospace mode.
I didn't try (yet) to change the character mode "in flight", but believe that this is possible. But i think that you can use only one mode/size for a time, that is: you cannot mix typefaces / sizes at the same screen.
There's no way with ncurses, but certain terminals support changing fonts. For xterm, for example:
$ echo -e '\x1b]50;-misc-fixed-medium-r-normal--20-200-75-75-c-100-iso10646-1\a'
XTerm Control Sequences
Change Terminal Font Size with C++
For extra credit, the professor wants us to use bolding and/or underlining to text output in the current project.
The example he gave was b\bb o\bo l\bl d\bd is displayed as b o l d
Following that example, I marked up SPACE as
printf("\033[7mS\bSP\bPA\bAC\bCE\E- move forward one page\033[0m");
I'm also implementing reverse video by enclosing strings within \033[7m and \033[0m fields. The reverse video inverts the colors of the line appropriately, but doesn't seem to be affecting the bolding, since both strings with and without the reverse video are not bolding.
Could it be the standard shell used in Ubuntu 10.10 that is at fault?
I agree about using curses, but given your starting point ....
I think you want to use the 'bright' feature of VT100 for the bold, ESC[1m
You can probably find better doc on VT100 codes, but using this page I found the codes. ANSI/VT100 Escape Codes
I hope this helps.
Unless you're just trying to be masochistic, try using curses (or ncurses) instead.
// warning: Going from distant memory here...
curs_attron(A_INVERSE); // maybe A_REVERSE? I don't remember for sure.
curs_addstr("SPACE - move forward one page");
curs_attroff(A_INVERSE);
I am using Visual Studio 2010 Pro for simple C programming, I would like to know how I can provide input to the program without having to manually do so. The enviroment I am used to working is your standard commandline Unix enviroment. Once I compile a C file call "inputsInts" it becomes "a.out" and to test input I would type:
The easy way
echo 1 2 3 4| ./a.out //to provide input
The number of ints input was 4 //output
The easier way
more input.txt| ./a.out //to provide input
The number of ints input was 4 //output
The tedious way
./a.out
//now I would manually type
1 2 3 4 s //in this case I have to type a letter to move on
The number of ints input was 4 //output
Hard is how I have to do it in Visual Studio 2010. I would like to be able to just input in an area the input ahead of time or at least have it read a text file. Obviously I can't test large sets of data by manually typing it in. At the moment I am just doing the coding in VS2010 and going to the unix enviroment to do most testing. I would like to stay in the VS2010 enviroment until I am ready to do a final test in Unix.
I have altered the question quite a bit since I first posted, so the original answers may seem off a bit. Again I appretiate everyone's time and help.
This is just the simple code for an example:
#include
int main () {
int x, n = 0;
while (scanf("%d", &x)==1)
n++;
printf("The number of ints input was %d\n", n);
return(0);
}
You need to create a "Console Application" when you start a new Visual Studio project. This will give you a program that runs from a Windows Command Prompt window, otherwise known as the Cmd window after the name of the shell program that runs underneath it. The command window is located under Programs->Accessories in Windows XP, not sure about other versions of Windows. Once you open a command window, things will work similarly to what you're used to on Linux.
cd MyProject
echo 1 2 3 4|.\MyProject.exe
MyProject.exe <input.txt
The cmd.exe shell has a pipe operator that works similar to the Unix pipe operator. It has some quirks in some versions of Windows but in general, you should be able to do many of the same things with it.
You can run your program pretty much the same way at the Windows command line, the only obvious difference being that you need to specify the correct executable name instead of a.out.
To do roughly the same from inside the VS IDE, you'd probably need to store your sample input in a text file, and then specify something like < sample.txt as the arguments to supply to the program in the project's debug settings.
In the Windows Shell (cmd.exe) you can use a pipe similar to linux for commands like
dir|more
Outside the shell, you're talking about a GUI environment (as in Linux's GUI) so passing info from one program to the next will be a bit different.
However, you can achieve similar functionality using pipes (shared memory in Windows). See here for a full explanation with examples from the folks at Microsoft Developer Network:
http://msdn.microsoft.com/en-us/library/aa365780%28v=VS.85%29.aspx
Or if you're feeling to lazy to poke around, here's an example of transactions on named pipes:
http://msdn.microsoft.com/en-us/library/aa365789%28v=VS.85%29.aspx
...or you can simply dump and read from output files.
(Both of these methods are similar to those used with Linux programs)
More info on your specific needs would be helpful.