Text from URL to String - C++ [duplicate] - c++

This question already has answers here:
Downloading HTTP URLs asynchronously in C++
(4 answers)
Closed 8 years ago.
What I want my program to do is to download data from URL(this will be some text, code, whatever) and save it to string in C++. How do I do this? I googled a lot and found absolutely nothing, except using some classes that exist only on Linux. I'm working on Windows so I don't accept answers such as: use curl.h . Thanks

There are some POCO libraries that make it very easy. Here is some code that reads a single line of text from a website:
#include "Poco/URIStreamOpener.h"
#include "Poco/Net/HTTPStreamFactory.h"
Poco::Net::HTTPStreamFactory::registerFactory();
Poco::URIStreamOpener& opener = Poco::URIStreamOpener::defaultOpener();
try
{
std::auto_ptr<std::istream> reader(opener.open(myURL));
reader->getline(line,sizeof(line));
}
catch(...)
{
}
And that's all there is to it. The try...catch is there in case of a bad Internet connection.

Related

C++ console insert text into displayed text [duplicate]

This question already has answers here:
Rewinding std::cout to go back to the beginning of a line
(3 answers)
Closed 4 years ago.
How can I rewind std::cout back to beginning of line and insert text without overwriting exiting one ? Can it be done using just standard c++ functions, or do I need low-level OS functions for console to do this ?
EDIT: I'm writing a simple telnet client. So when a message is received it should be appended at the top and user imput should not be overwritten.
No, you can't do this, and it's considered useless in console.
There is a function named std::seekp for all basic_ostream based class. But when you apply this to cout, no effect at all but failbit is set.
Use std::cout << '\xd' this line will output a carriage return which will fulfill your requirement. This this line will overlap your previous entry.

How to open protected Windows share using Python? [duplicate]

This question already has answers here:
Python - How to open Windows share using user name and password
(4 answers)
Closed 5 years ago.
How do I open Windows share with credentials using Python on a Windows machine?
I'm currently doing this:
share = open(r'share#username:password','r')
i used
subprocess.call(r'net use z: \windows_server\share /user:#username password', shell=True)
to mount the share and
subprocess.call(r'net use z: /delete')
to delete it.This is the most hassle free and efficient way. Thanks everyone for the help!!

How can I insert an input in sublime compiler [duplicate]

This question already has answers here:
Sublime Text with console input for c++ programs
(2 answers)
Closed 8 years ago.
I started to use sublime text 2 editor today to write c/c++ programs, but when I run this code
#include <cstdio>
int main(){
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
I am expecting the compiler to ask for an input.
but instead, it just prints the initial value of (n) which is garbage.
How can i insert an input ?
I suppose what you want to do is input data in the same panel where Sublime Text show results, well friend, you can't.
The only thing you can do is configure Sublime Text for running the compiled program in an external terminal. The link chris provide you is a good start.
On the other hand when you say: expecting the compiler to ask for an input, is a conceptual error. The compiler is not whom is expecting the input. The input is being expected by the compiled program.

How to get the current default set language of the current Windows OS for version 7 and later versions using C++? [duplicate]

This question already has answers here:
How to get OS language using C++ API?
(3 answers)
Closed 8 years ago.
How to get the current default set language of the current Windows OS for version 7 and later versions using C++?
To deal this, I've tried to look for in the net but without any result.
Any brilliant suggestion, please?
Thanks a lot!
You can call GetUserDefaultLocaleName to get the locale
string const s = setlocale( LC_ALL, "" );
cout << s << endl;
… is one way.
Update: The above does not retrieve the “preferred” language set in the Control Panel. E.g. on my laptop it says Norwegian in spite of a preference for US English. Following the documentation I tried GetSystemPreferredUILanguages, but it still reported Norwegian. However, GetThreadUILanguage reports US English, so presumably that's it.

Execute file in another directory [duplicate]

This question already has answers here:
system("c:\\sample\\startAll.bat") cannot run because of working directory?
(4 answers)
Closed 9 years ago.
Consider the following:
I have a c++ program in C:\Documents\myProgram.exe
With this code in it:
system("start C:\\somefolder\\start.bat");
That will start the target file (start.bat) in C:\Documents\ instead of `C:\somefolder\'.
My question is, how do I execute the file in it's own directory instead of myProgram's directory?
In theory this is what I want to accomplish using c++:
cd C:\somefolder\,
start start.bat
If you're on windows anyway, use ShellExecute, you can set more things and launch even documents, links etc.
To do this you can do one of two things (that I found).
A) You can use chdir() in unistd.h; see http://pubs.opengroup.org/onlinepubs/7908799/xsh/unistd.h.html
or
B) You can use something called the File System Interface, from the GNU library, for more advanced stuff; see http://www.gnu.org/software/libc/manual/html_node/File-System-Interface.html#File-System-Interface.
Anyway, best of luck, I hope you find something that will work!