I'd like to develop character based program with Tiny C Compiler.
The putc works fine, but I want to print the character in a specific place on the character screen. Like in turbo pascal with gotoxy. Is there such a possibility?
I'm looking through a bunch of header files, but couldn't find one.
#include <stdio.h>
int main(int argc, char **argv) {
// gotoxy(1,15); // ???????
putc(65,stdout);
}
Related
I am new and I know how to color output only in Unix/Linux systems:
#include <stdio.h>
int main(void) {
printf("\033[1;31mRed Message\033[0m.");
}
But this is not works in Windows cmd.exe, only in Unix terminal.
I am writing cross-platform app and want to know how can I do this in Windows cmd.exe too.
This also does not works:
1.
#include <stdio.h>
int main(void) {
printf("%c[1;31mRed Message%c[0m", 27, 27);
}
2.
#include <stdio.h>
int main(void) {
printf("[1;31m Red Message [0m");
}
This works, but I think this is just a bug:
If I type system(""); before printf then it works.
#include <stdio.h>
int main(void) {
system("");
printf("\033[1;31m Red Message \033[0m");
}
Thanks
If you want to make your library crossplatform, I would use the following approach:
Have a library, with the same functions, let's say:
void printInRed(const char* string). (In a headerfile)
After that you write two or more implementations.
One for windows:
//TODO: Errorchecking
void printInRed(const char* string){
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
//TODO: Extract magic number
//See https://stackoverflow.com/a/4053879/13912132
SetConsoleTextAttribute(hConsole, 12);
puts(string);
}
And another one for unix-like OS:
//TODO: Errorchecking
void printInRed(const char* string){
printf("\033[1;31m%s\033[0m.", string);
}
Then you can check at compile time, which version to compile.
The first approach is to use #ifdefs, but this will make the code a bit messy.
Another approach would be to use a build-system like CMake to select at build time, which one to build. A buildsystem requires a bit of learning, but will help you to make maintaining a crossplatform library simpler.
Using:
Code::Blocks Software
Teach Yourself C book
None of "command line argument" example programs work. They either crash or execute with all variables with 0 value or show similar results to the program below.
#include <stdio.h>
int main(int argc, char *argv[])
{
return 0;
}
The simplest of the example program is below.
#include <stdio.h>
int main(int argc, char *argv[])
{
int i;
for(i=1; i<argc; i++) printf("%s ", arv[i]);
return 0;
}
With a bit of googling I have found that I need to have a Project as a Console Application and then use Project -> set programs arguments, but I have no idea of what to do in the window that pops up.
If you have compiled your project as a console application, you can pass arguments by calling the program from the console (cmd.exe in Windows, terminal in Linux).
The window Project -> set programs arguments simply asks you what arguments do you want to pass to the program when you run the program from Code::Blocks (using the green arrow).
Simply add your arguments in the "Program arguments" text box.
#include "hmap.h"
int main(char* argv[], int argc)
{
printf("%s", argv[0]); <---- fails here
system("pause");
fileOpen(argv[1]);
return 0;
}
I am using MSVS 2012. I'm wondering if I'm using the command line arguments wrong. The text file is in the same folder. All my header file has is the #include libraries I will using, some #define's I'll be using, and extern function prototypes.
When I run the program it says "expand.exe has stopped working...."
I usually program in a Linux environment using GCC but I'm trying to learn MSVS environment. Getting a little frustrated on how much of a hassle to input command line arguments :.
I think the arguments for main() are around the wrong way.
That is, the first argument should be the argument count (argv), and the second one the argument vector (argv).
int main(int argc, char* argv[]) {}
It fails because a subscript should be used only with an array or pointer.
I want to write a a C program that prints its location.
For example if i put the program exe file to D:\myfolder\myc_prog, it should print the same location D:\myfolder\myc_prog and if I put that exe file to the location E:\mynewfold\ , it should print the updated location E:\mynewfold.
Actually, I have no idea how to do it that's why I'm not able to provide much details for this question.
Since you're using Windows, GetModuleFileName should do the trick. Just pass NULL for the hModule parameter. Be sure to read the documentation carefully if you want to handle long file names (and you typically do). You'll also have to strip the name of the executable to get the directory path. A quick-and-dirty way to do so is to remove everything after the last \.
#include <Windows.h>
#include <stdio.h>
int main(int argc, char *argv[]){
char buff[256];
if(GetCurrentDirectory(256, buff)){//get current directory
printf("%s\n", buff);
}
return 0;
}
Basically the title is self explaining. I'm programming in C and i use fgets as the input function but i do not want that control characters get printed.
fgets() is rather simple, and doesn't offer you much control over what appears on the screen. I don't think that it's possible to do this. You may want to look into something more powerful - like readline.
Yes, as other post says, readline is your best bet. Its simple too. If you are on Linux, it should already be installed. try the following:
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
// compile as: gcc <file>.c -lreadline -lcurses
int main (int argc, char *argv[])
{
char *input = readline("Enter words: ");
printf("\n Input: [%s]\n", input );
return 0;
}
--
HTH.