<< split from this thread >>

I am a student in Bogazici University/Turkey. We have a project and I want to change the cursor position but there is a problem. Our teachers are using Microsoft Visual C++ 2005 Express Edition. In this compiler there is no windows.h header. What can I do?

>Can you please help me on calling interrupts in that standard?
Didn't you already ask that question, or was it someone else?

>By I think, one of the h files can be used for this purpose.
windows.h, and you have to implement the functionality yourself:

#include <windows.h> 

void gotoxy ( short x, short y )
{
  COORD coord = {x, y};
  SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ), coord );
}

There is no way VS C++ doesn't contain windows.h in its include folder; when the pigs fly...

You had better search for that file using windows' search tool or using something like Google Desktop.

I am a student in Bogazici University/Turkey. We have a project and I want to change the cursor position but there is a problem. Our teachers are using Microsoft Visual C++ 2005 Express Edition. In this compiler there is no windows.h header. What can I do?

visual c++ express does not ship with the platform sdk (for which windows.h is the main header). however, you can download and install the platform sdk for free. and you need to set up the visual c++ environment to use it. for clear step-by-step instructions on how to do this, see:
http://msdn.microsoft.com/vstudio/express/visualc/usingpsdk/

It seems that you like to write some console/terminal applications. Please try to understand me now. Standard C doesn't provide any features for that. Yes it's somewhat stupid to be able to write only some command-line programs, for example, while learning C. But C standard library is anyway only a fraction of POSIX, so it makes no sense to be restricted with C standard library. And such console/terminal programs can be written using only standard C and POSIX. We need really two things -- non-canonical input and clear screen. The code below shows how we can set the non-canonical input using termios.h, this code clears screen and prints immediately any character which you enter. Clearing screen is somewhat more tricky, because POSIX doesn't provide any function or control sequence to clear the screen. It provides a command "tput clear" for that though. So the only way to clear screen quickly using only the POSIX features, is to get the control sequence which "tput clear" outputs, and then print it every time to clear screen. The code below gets that control sequence using popen.

And now again, how can we move the cursor. POSIX does not provide any control codes, functions or utilities to move cursor. And if to think about it more deeply, *you do not need to move cursor*, the gotoxy kind of functions for console are somewhat similar to goto functions in c, both seem to be convenient, but would cause a bad programming. It is that whenever we have a user interface, we need windows, consider things like a message box which should pop up. And always when we have windows, there must be a function for every window, which rewrites all its content in any particular moment, because a window may become covered by another window etc. It is the general philosophy, and even not difficult to implement, it's just a matter of approach. And having such function, we would not need to move cursor any more, we just update an area after every change, which is normal, most text editors update screen after every letter you type.

Anyway, writing console applications makes sense only for simple programs, it would make no sense to write a program with complicated user interface without using graphical UIlike GTK. And it is that most people write only simple programs, why really should they then learn things like curses, only to write a simple interactive program.

If you use Windows, depending on your compiler, you may have to use Windows functions for non-canonical input and clearing screen. But when you do even that, it's better to still follow all that, then it would be easy to port your program to pure POSIX and it can easily run on any platform.

#!/usr/bin/tcc -run
#include <stdio.h>
#include <termios.h>

int main ()
{
        struct termios ts0, ts1;
        char cls [FILENAME_MAX];
        FILE *f;

        f = popen ("tput clear", "r");
        fgets (cls, FILENAME_MAX, f);
        pclose (f);
        tcgetattr (0, &ts0);
        ts1 = ts0;
        ts1.c_lflag &= ~ECHO;
        ts1.c_lflag &= ~ICANON;
        tcsetattr (0, TCSAFLUSH, &ts1);
        fputs (cls, stdout);
        while (1) putchar (getchar ());
        tcsetattr (0, TCSAFLUSH, &ts0);
        return 0;
}

I add this code. But Visual C++ Express Edition doesn't have terminos.h too. where can i get this? Maybe in SDK?

It depends what you need to do. If you can do it with canonical input, you can just rewrite the screen every time something changes, the problem with this is that it's slow, and is not a good solution when something changes very often, especially on Windows console which is extremely slow. And yes, without cleaning screen it may show some flickering, it's not exactly a good quality, but then for some university tasks it may even be enough, and the program written like that can later be very easily ported. The canonical input is mostly the biggest problem, because many such interactive programs also need some moving around, like for selecting something, and doing that with canonical input is just much too inconvenient. BTW, system ("cls") clears the screen in Windows, but it can only be used when it will not occur very often, and i didn't really try whether the cls command in Windows also sends some control sequence to console, though i doubt that it doesn't.

It seems that you like to write some console/terminal applications. Please try to understand me now. Standard C doesn't provide any features for that. Yes it's somewhat stupid to be able to write only some command-line programs, for example, while learning C.

But it's even stupider to try to learn C and a very non-standard library like POSIX.

But C standard library is anyway only a fraction of POSIX, so it makes no sense to be restricted with C standard library.

Makes total sense. If learning C, you learn C that can be used everywhere. Learn C and POSIX, now you can only write code that has POSIX, and most jobs you are likely to get won't have it.

And such console/terminal programs can be written using only standard C and POSIX. We need really two things -- non-canonical input and clear screen. The code below shows how we can set the non-canonical input using termios.h, this code clears screen and prints immediately any character which you enter.

So now we not only add a non-standard library like POSIX, but now another non-standard header the is available only on Linux. Really helps on Windows... :rolleyes:

POSIX does not provide any control codes, functions or utilities to move cursor. And if to think about it more deeply, *you do not need to move cursor*, the gotoxy kind of functions for console are somewhat similar to goto functions in c, both seem to be convenient, but would cause a bad programming.

As is POSIX ;)

It is that whenever we have a user interface, we need windows,

I've been writing code long before any windowing even existed. It is not necessary. Windowing is difficult to do properly, and when learning C it is best to concentrate on C, not on overly bloated windowing.

Anyway, writing console applications makes sense only for simple programs, it would make no sense to write a program with complicated user interface without using graphical UIlike GTK. And it is that most people write only simple programs, why really should they then learn things like curses, only to write a simple interactive program.

To paraphrase what I said before, extremely complex programs have been written long before any GUI existed. It all depends on what you want. Cute graphics and overblown executable or something that works well so you can concentrate on learning what you need to learn.

I add this code. But Visual C++ Express Edition doesn't have terminos.h too. where can i get this? Maybe in SDK?

That's because Windows doesn't have terminos.h. It's a Unix thing.

There is no standard way to use cursor control in C. If it's a requirement, your instructor will explain how he wants it done.

It seems that what concerns the programming, there are so many opinions that it's even not worth to discuss. termios.h is a POSIX header, and POSIX is standard, the only standard of an operating system, and even Microsoft has some POSIX kit, even Windows somewhat follows that standard. Windows are a programming concept, not only graphical windows what we see on the screen. But in Windows, it's really likely the only way to get any kit which has windows.h, and use non-canonical input and moving cursor from there. windows.h is about windows api, it's not any standard though. But of course it's necessary to ask what kit the instructor has.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.