So I have this simple code to download a file from an url opening the browser
#include <iostream>
#include<Windows.h>
#include<string>
using namespace std;
int main()
{
string dwnld_URL = "http://www.url.com/downloadpage";
ShellExecuteA(NULL, "open", dwnld_URL.c_str(), NULL, NULL, SW_SHOWNORMAL);
return 0;
}
But I want the file to go in the current directory instead of going in the default download folder. Is there any way I can do this?
if you're on windows, you can use
#include <iostream>
#include<Windows.h>
#include<string>
#pragma comment(lib, "urlmon.lib")
using namespace std;
int main()
{
string dwnld_URL = "http://www.url.com/downloadpage/filename.txt";
string savepath = "C:\\tmp\\filename.txt";
URLDownloadToFile(NULL, dwnld_URL.c_str(), savepath.c_str(), 0, NULL);
return 0;
}
Read more about HTTP and URLs first.
You want some HTTP client library; you could consider libcurl, but both Qt and POCO have some functions for HTTP client. And probably Windows might have some specific functions around that.
All three libcurl, Qt, POCO are free software libraries, and can run also on Linux and POSIX systems.
If you need an HTTP server library (which does not seem the case), you could find some also (e.g. Wt, libonion, ...)
Regarding your comment (which should have gone into your question) "but how can I know"?, an intuition could be, for remote data access, to focus on protocols.
Whatever library (or framework) you are using, you'll need to spend many hours or days to study it and read its documentation and tutorials.
Related
So basically i'm trying to Download a File from a URL (NOT FROM A HTML/PHP) from a direct download link, example: https://cdn.discordapp.com/attachments/0/0/file.txt. I want my program to download a certain file from a direct download link in the same directory where the application was ran, i know this is possible in C# using WebClient.DownloadFile but I'm not exactly sure on how to use something similar in C++. If you could help that would be amazing, thank you and have a great day.
In my opinion, you could use URLDownloadToFile to download files from the url.
I opened the link you gave but the webpage reported an error and did not find any txt. Therefore, you could refer to the following example:
#include <iostream>
#include <urlmon.h>
#pragma comment(lib,"urlmon.lib")
using namespace std;
int main()
{
HRESULT hr = URLDownloadToFile(0, L"http://pic104.nipic.com/file/20160715/6171480_185807154956_2.jpg", L"D:\\testfolder\\sky.jpg", 0, NULL);
if (hr == S_OK)
{
cout << "ok" << endl;
}
return 0;
}
Of course, you could use the third-party library, such as libcurl.
I am creating a C++ Application using Boost Library, so for I have developed modules but stuck here.
I want to Create and modify System Variables(Or Local Environment Variables that persist even after process has terminated) using boost library.
There are methods like
setenv()
putenv()
getenv()
but they make local changes and the value gets disappeared after the process has terminated.
Well that's not the real issue. The real problem arises when i want the same code to run on linux and mac platform.
Can I make a cross platform code using C++ with Libraries that can create and modify and delete the path variables ?
If not
Can I make a code for specific platform say Windows with the above requirements.
As of now I have this code that sets Environment Variable for windows but it give error
#include <string>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/stream.hpp>
#include <process/boost/process.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/system/error_code.hpp>
namespace bp = boost::process;
namespace bpi = boost::process::initializers;
namespace bio = boost::iostreams;
int main()
{
char* str="SETX Name Value";
bp::pipe p = bp::create_pipe();
{
bio::file_descriptor_sink sink(p.sink, bio::close_handle);
boost::filesystem::path p("C:/Windows/System32/cmd.exe");
boost::system::error_code ec;
bp::windows::execute(bpi::run_exe(p),
bpi::set_cmd_line(str),
bpi::bind_stdout(sink),
bpi::set_on_error(ec)
);
}
bio::file_descriptor_source source(p.source, bio::close_handle);
bio::stream<bio::file_descriptor_source> is(source);
std::string s;
is >> s;
std::cout << s << std::endl;
std::cin.get();
return 0;
}
This has nothing to with Boost, and actually very little with environment variables.
It has to do with System configuration instead.
So, given you have the required permissions to alter the user/system environment you can use these methods to make the changes stick:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx
Most of the samples around favour tools like Windows Scripting Host, PowerShell and .NET to do the job:
Programmatically modifiy environment variables?
But you can probably translate it to C++ if you must.
PS. On Linux, you'd alter some files like /etc/profile, /etc/default/... etc.
I am working on a simple program to copy all files in folders from one drive to another using C++. Using the Windows API function CopyFile(). I have used the following code:
#include <iostream>
#include <windows.h>
int main()
{
std::cout << "File Copier Version 1";
CopyFile("U:\\whateverfile.file","U:\\whateverfile2.file",0);
return 0;
}
What is the best way to handle copying an entire directory and all the files in it? Any other advice on this? Problems I may encounter?
If you wish to do it on Windows with progress display and other features, without putting too much effort into it, look up IFileOperation::CopyItem. However, it requires Vista or later.
I am writing an algorithm that wants to check if google-chrome or has the same version as the input given by the user. for that I need a way to check what version google-chrome has. I am using a linux machine to program but I want to make it work at home where I use win 8.1
Is there a way to check in C/C++ what the version of a program is?
I thing it is best to get the awnser in a string because then i can just compare with
if(strcmp(version, input)=1)
Thanks for reading.
PS. I started in C++ but I can change, even to java if neccesary
here is the basic version of what i have now:
#include <iostream>
#include <string>
using namespace std;
#define x 256;
int main(){
std::string version;
std::string input;
//get version
if(strcmp(version, input)=1){
//versions are equal
}
//chrome needs to be updaded
return 0;
}
You can launch a terminal process from C with the popen() command. You'll need to include the stdio.h header. Here's a code snippet that might help you:
FILE *pd = popen("google-chrome --version", "r");
char output[50];
fgets(output,50,pd);
pclose(pd);
In the output array you'll get something like "Google Chrome 25.0.1364.97"
I just made a program in Qt that creates a folder in a specific diretory.
the code is:
QDir directory;
directory.mkdir("Sample");
my problem is how could i convert the folder to a shared folder using Qt codes?
Or is there a way to create a shared folder using Qt??
You can share a directory using NetShareAdd. As far as I know, Qt doesn't provide anything with the same basic capability as NetShareAdd.
Edit: here's a quite bit of demo code:
#include <windows.h>
#include <lm.h>
int main() {
SHARE_INFO_2 info = {0};
info.shi2_netname = L"test_share";
info.shi2_type = STYPE_DISKTREE;
info.shi2_permissions = ACCESS_ALL;
info.shi2_max_uses = -1;
info.shi2_path = L"C:\\a\\b\\c";
NetShareAdd(NULL, 2, (BYTE *)&info, NULL);
return 0;
}
Note that NetShareAdd (like most of the Net* functions) is only available in a "wide" version that uses wide character strings.
This seems like it would be operating system dependent; Qt's abstraction of the OS-native directory functions isn't likely to be concerned with such a thing.
You'll probably want to look into your OS' specific methods for changing the "shared" status of a directory. On Windows, this might involve using WMI.