tags:

views:

119

answers:

3

Hello, I was wondering if you can add a link to a website in a C++ program running in the CMD Prompt type window (No GUI)

If it's possible can some one please give me a few examples?

Thanks.

+3  A: 

You mean output text in the command prompt that the user can then click on? No, not unless the terminal supported it. Linux terminals usually autolink text that matches a URL pattern, so you could just printf("http://stackoverflow.com/\n"); and it would be clickable, but that's up to the terminal, not your program

Michael Mrozek
Darn, okay thanks any way.
TheEliteOne
+1  A: 

When you write 'direct link' it is not clear if you mean clickable text or a means to open a url. At any rate, command line programs usually respond to command line parameters. Your program could open a url in the default browser in response to a command line flag. On Windows, you could call ShellExecute - on other systems, system might be appropriate.

sean e
+1  A: 

It depends. In Windows, for example, yes, it's entirely possible, though somewhat non-trivial. You can receive mouse events via ReadConsoleInput, so in theory it's a fairly straightforward matter of reading the input event, and if it's a mouse click over the area you've defined as a link, you direct the user to the link as you see fit -- if you want to display the web site in text mode, that's possible (though, again, distinctly non-trivial). If you want to start up the user's normal web browser, that's a lot simpler (normally just ShellExecute the URL).

In reality, the details get a bit ugly. You have to enable mouse input for it to work at all. ReadConsoleInput gives you an INPUT_RECORD, which is a union of a number of different input record types, one of which is a mouse input record. By the time you get to react to a mouse click, your code is nested fairly deeply. None of it is unmanageable by any means, but unless you already have a fair amount of experience with Windows console programming, it might easily take most of a day (maybe even a bit more) before you have it working, rather than the hour or two you'd initially guess.

That, of course, is strictly for Windows -- if you ever want to port the code to another system, I'd guess there's a pretty good chance you'd be looking at a complete rewrite. For GUIs there are a fair number of cross-platform libraries, but text mode mouse operations aren't nearly so well supported.

Jerry Coffin
Wouldn't GPM be the linux equivalent? http://freshmeat.net/projects/gpm/
Ben Voigt
On Unix and Mac systems, you could call `system()` or `popen()` (or if you want to go crazy and be perfect about it, use `fork()` and `exec()`, which give you lots more control) to launch a browser with the URL as a command-line parameter. On the Mac, the command `open` will launch the user's browser if given a URL argument, so something like `std::string cmd("open '"); cmd += url; cmd += "'"; system(cmd.c_str());` might do the trick. I don't know how to launch the user's preferred browser in Linux; it may be different for Gnome vs. KDE. P.S. *Make sure you properly escape the URL.*
Mike DeSimone