I am using ifstream to open a file and then read from it.
My program works fine when i give location of the local file
on my system.
for eg
/root/Desktop/abc.xxx works fine
But once the location is on the http server the file fails to
open.
for eg
http://192.168.0.10/abc.xxx fails to open.
Is there any alternate for ifstream when using a URL address?
thanks.
There are no utilities in the standard C++ library for accessing data via http protocol.
There are 3rd-party libraries though:
Libwww
libcurl
Another option is to have a virtual filesystem that maps remote http files as local files. This way you don't have to modify your application to access http. Something like http://okmij.org/ftp/HTTP-VFS.html
ifstream will not read files off of an HTTP Server. It will only read local files.
The f in ifstream is for file, not socket.
You need to make an HTTP GET Request and then stream the response, this is a totally different operation.
Consider using boost::asio, or similar. [Examples]
Update
Since the web server is on your local area network (judging by the IP address - not sure why people still insist in using those in these heady days of DNS, but that's by the by), you could probably mount the filesystem containing the desired file on your local machine, using NFS or similar. Then you'd be able to perform file operations, such a reading with ifstream.
ifstream only works with files accessible on the filesystem, not on web servers.
Related
There is a detection in VirusTotal: remote System Discovery T1018: Reads the hosts file. For testing I want to get rid of this detection. I suppose that it is libCurl, who reading this file.
Is it possible to disable libCurl from reading hosts and what are consequenses of that?
You can set the CURLOPT_RESOLVE option to explicitly specify the IP address of the remote host in the request. This will bypass the need for libCurl to resolve the hostname using the hosts file.
I'm using the following line to download a file, and when I do that, it's not downloading the most recent file.
HRESULT hr = URLDownloadToFile(NULL, _T("http://example.com/users.txt"), _T("users.txt"), 0, NULL);
On the first run, users.txt has 3 names in it, if you were to remove a name, and run it again it still downloads with 3 names.
I'm using remove("users.txt); to remove the file prior to download.
It is probably operating system specific, or at least you need a library for HTTP client side.
You need to read a lot more about the HTTP protocol. The formulation of your question makes me believe you don't understand much about it.
On some OSes (notably Linux and POSIX compliant ones), you can use libcurl (which is a good HTTP client free software library)
URLDownloadToFile seems to be a Windows specific thing. Did you carefully read its documentation? It is returning some error code. Do you handle hr correctly?
You can probably only get what the HTTP protocol (response from web server, for a GET HTTP request) gives you. Mostly, the MIME type of the content of the URL, the content size, and the content bytes (etc... including content encoding etc...). The fact that the content has 3 names is your understanding of it.
Try to read more about the HTTP protocol, and understand what is really going on. Are any cookies or sessions involved? Did you try to use something like telnet to manually make the HTTP exchange? Are you able to show it and understand it? What it the HTTP response code ?
If you have access to the server (e.g. using ssh) and are able to look into the log files, try to understand what exchanges happened and what HTTP status -i.e. error code- was sent back. Perhaps set up some Linux box locally for initial tests. Or setup some HTTP server locally and use http://localhost/ etc...
I am using ColdFusion 8.
I am trying to write a file on a networked path Windows.
// THIS WORKS
CatalogDirectory = getDirectoryFromPath("E:\INETPUB\WWWROOT\AVCATALOGS\AVCAT\");
// THIS DOES NOT WORK
CatalogDirectory = getDirectoryFromPath("\\ourserver\e$\InetPub\wwwroot\AVCATALOGS\");
I can't find any good documentation on what you CAN'T do.
// TOO VAGUE
http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_e-g_36.html
Is there a way to write copy a file from one server to another server on networked drives?
you must be running ColdFusion as a network user and that user must have permission to access the server you are connecting to.
I know that we can retrieve a webpage content through curl http://curl.haxx.se/ but is there any native way to retrieve the content of an webpage using c++ without using any library?
You will always need some kind of library in order to establish a network connection (I count OS APIs as libraries). That aside, you would have to:
establish a connection to the server
send a http request
receive and handle the http response
You can implement these steps by hand, but that really is a pain, especially because http is quite a complex protocol (even if you only implement the stuff you actually use, enough remains).
If you use Windows, you can use functions below
InternetOpen() - Initializes an application's use of the WinINet functions.
http://msdn.microsoft.com/en-us/library/aa385096(VS.85).aspx
InternetOpenUrl() - Opens a resource specified by a complete FTP, Gopher, or HTTP URL.
http://msdn.microsoft.com/en-us/library/aa385098(VS.85).aspx
InternetReadFile() - Reads data from a handle opened by the InternetOpenUrl
http://msdn.microsoft.com/en-us/library/aa385103(VS.85).aspx
InternetCloseHandle() - Closes a single Internet handle
http://msdn.microsoft.com/en-us/library/aa384350(VS.85).aspx
Hope it helps
PS: or you can use a more convenient function
URLDownloadToFile() - Downloads bits from the Internet and saves them to a file.
http://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx
so I have a folder "Assets" which is in the same folder with my server executable. I want to return file from it (ofcource if file exists) to any user that connects to my server via tcp and sends a filename (generally I wish to see a asinc, nonblocking tcp server made using boost which would simply return any file form folder near to the executable). How to do such thuing with boost? (I use VS 2010 for compiling)
This sounds like a job for a web server. This is what web servers do. They are good at it.
Consider using one of the zillion web server apps out there or integrate a little web server into your program:
- https://github.com/cloudmeter/pion
There are more linked from this question:
- https://stackoverflow.com/questions/175507/c-c-web-server-library