How do you copy/paste from the clipboard in C++? - c++

I'm still a C++ newbie who has only recently learned some file manipulation. I looked it up online and the codes given are way beyond my current skill. Is there a simple way to do this, or are there any good tutorials that can explain this from the very basics?

In windows look at the following API:
OpenClipBoard
EmptyClipboard
SetClipboardData
CloseClipboard
GetClipboardData
An extensive discussion can be found here.
Obviously this topic is strongly operating system related. And if you are using some framework (ie MFC/ATL) you generally find some helper infrastructure. This reply refer to the lowest API level in WIndows. If you are planning to use MFC have a look here, if you prefer ATL look here.

There is no cross-platform way to do this in C++
Now that we have that out of the way, Felice Pollano's answer provides the Windows API so you can manipulate the clipboard in Windows.
Apple provides an example application named ClipboardViewer and an entire reference to the NSPasteBoard and the functionality it provides.
As for Linux, it depends on what windowing manager you are running.

There is a cross platform way to do this in C++, provided you are willing to use the Qt Library.
A solution for this is provided here:
https://stackoverflow.com/a/40437290/2158002

You can use ClipboardXX library for copy and pasting simple texts.
Just download clipboardXX.hpp from github and copy it to your project path. Then follow its examples:
#include "clipboard.hpp"
#include <string>
int main() {
clipboardxx::clipboard clipboard;
// copy
clipboard << "text you wanna copy";
// paste
std::string paste_text;
clipboard >> paste_text;
}

If you are looking for a simle way to do this : simulate the keyboard combination ctrl + v and you are done with it. On all platforms.

Related

Alternatives to inotify to detect when a new file is created under a folder

I am developing my program on Linux, is there a programmatic way to detect when another application creates/copies a file under/to a specific folder. I want to detect the new file as fast as it is created and I would like to process the file.
As far as I researched I can accomplish this using inotify. Are there any better alternatives?
inotify is the proper API provided by the Linux kernel. Your toolkit may have convenience on top of it, e.g. KDirWatch from libkdecore, but that uses inotify internally.
Using API from a toolkit is a good idea when your program is cross-platform.
http://www.highscore.de/boost/dir_monitor.zip on http://en.highscore.de/cpp/boost/asio.html is a cross-platform C++ Boost solution, though I haven't tried it yet.
http://boost.2283326.n4.nabble.com/ASIO-file-monitoring-help-td4645105.html has code using it that is wrong, the fix looks to be to make a few more objects the author assumed could be temporary permanent instead.
http://man7.org/linux/man-pages/man7/fanotify.7.html is another option
This is a nice article which sums all methods http://www.lanedo.com/filesystem-monitoring-linux-kernel/

Detecting new device insert in C++

I've tried to find a way to detect when a new device (like a USB) is inserted into a computer, but everything I've seen required MFC, which I don't have. Is there a way to do this without MFC? If not, I understand, but I haven't seen anything of the like in my Google searches.
Thanks,
C++ by itself does not have any platform-dependent hardware-level functionality. You will need to use some API, like Win32 or MFC or .NET on Windows.
You can do this using libusb and there's a port of libusb for win32, so you might have some luck using that instead of MFC, with the added bonus of being more portable.
Handle the WM_DEVICECHANGE message.
See http://msdn.microsoft.com/en-us/library/aa363480(v=vs.85).aspx
EDIT: Of course, this is Windows only, which the OP didn't specify. There's no way to do this without external libraries or platform specific APIs.

Is it posible to open a cind of browse for file window to get the filepath of a file in c++?

And if this is possible pleas explain how to do this.
If you are just looking for a path and are on Windows, you probably want something like SHBrowseForFolder. If you are using a framework that abstracts away a lot of low-level Windows API stuff (MFC, Qt, WxWidgets, etc), then it may have some other more convenient function or class that does the same thing.
If you're not on Windows, then I can't help except by suggesting you search the operating system's API and/or the documentation of whatever GUI framework you use.

Download a URL in C++

I want to be able to download a URL in C++. Something as simple as:
std::string s;
s=download("http://www.example.com/myfile.html");
Ideally, this would include URLs like:
ftp://example.com/myfile.dat
file:///usr/home/myfile.dat
https://example.com/myfile.html
I was using asio in Boost, but it didn't really seem to have the code for handling protocols like ftp and https. Now I discovered QT has more what I need (http://doc.trolltech.com/2.3/network.html).
It's tempting to make the switch to Qt, but it seems a bit heavy and intersects a lot of Boost functionality. Is it worth learning yet another API (Qt) or can Boost do more than I think?
Not a direct answer, but you might like to consider libCURL, which is almost exactly what you describe.
There are sample applications here, and in particular this demonstrates how simple usage can be.
I wouldn't go to Qt just for the networking stuff, since it's really not all that spectacular; there are a lot of missing pieces. I'd only switch if you need the GUI stuff, for which it is top notch.
libCURL is pretty simple to use and more robust than the Qt stuff.
You can use URLDownloadToFile.
#include <Urlmon.h>
HANDLE hr;
hr=URLDownloadToFile(NULL, L"http://www.example.com/myfile.html",L"mylocalfile.html",BINDF_GETNEWESTVERSION,NULL);
According to MSDN, BINDF_GETNEWESTVERSION - is a "Value that indicates that the bind operation retrieves the newest version of the data or object available. In URL monikers, this flag maps to the WinInet flag, INTERNET_FLAG_RELOAD, which forces a download of the requested resource".
The Poco Project has classes for cross-platform HTTP and FTP (and a lot of other stuff). There is overlap with boost. I recently found this, but have not used it.
You can use the URLDownloadToFile or URLOpenBlockingStream, although cURL, libcurl are the proper tools for that kind of jobs.
I got it working without either libcurl nor WinSock: https://stackoverflow.com/a/51959694/1599699
Special thanks to Nick Dandoulakis for suggesting URLOpenBlockingStream! I like it.

Programatically opening and closing a console

I'm writing a windowed program in C++, but I would like to have the option to pop up a console to output to from inside the program (such as various things that go on behind the scenes of my program, to see that everything is acting correctly). Is there an easy way to do this?
EDIT:
In this particular case I'm using sfml on windows, but for the purposes of this question it can be any API or platform (and platform independent solutions are best)
If you are talking about MS Windows, which your question does not make clear, you can use the AllocConsole API to create a console. for your app.
Edit: You say that it could be any platform, but this is not so as many platforms have no concept of a console. For this reason, a cross-platform solution is not possible.
There are Windows API functions to deal with console management. This might be a good starting point.
Its easy to just open a console with system("cmd.exe"); But the communication part is not so easy. My intuitive feeling tells me that there exists a third party that satisfied your need. Might be worth looking at win32api or AllocConsole API (if you are using .NET) before experimenting with 3rd party libs.