|
 |
|
 |
03-09-2005, 08:14 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 1
|
Copy a remote file
Hi there,
I was wondering if any of you gurus can help me with this. I am new to C++ and am developing on MicroSoft Visual Studio 2003 (Visual C++).
I am trying to simply read a file from a webserver and save it locally. My code works when I copy the file from a local location but throws a null pointer when I give the url of the file.
+++++++++++++++++++++++++++++++++++++++++++++++
FILE* fp1 = fopen("http://192.100.53.90/DimensionView/booking_info.txt","r");
FILE* fp2 = fopen("C:\\booking_info.txt","w");
while(!feof(fp1)) {
char mystring[100];
fgets(mystring, 100, fp1);
//fputs(fp2,*mystring,strlen(*mystring));
fputs(mystring, fp2);
}
fclose (fp1);
fclose (fp2);
++++++++++++++++++++++++++++++++++++++++++++++++
Is it that fopen is can only be used to obtain a pointer to local files only? I have searched and searched but cannot find how to read a remote file (rfopen? )
Any help would be great.
Thanks,
Rob
|
|
|
03-09-2005, 11:17 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
fopen() can only be used for local files, it dosn't know anything about connections through network layer.
In order to retrieve a file through the network, you will need to open a socket to the apropriate server, decide if that times out which means no connection, if it dosn't, then send a file_exist request through the socket, if thats a success you can open/fetch the file.
This however requires alot more code, than your fopen() call.
|
|
|
03-13-2005, 09:15 PM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 56
|
I would like to learn how to do this also. Is there a tutorial or sample code that shows how to do this? Thanks.
|
|
|
03-14-2005, 04:39 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
I havn't got time right now to come up with an example, but tomorrow I'll work on one.. Perhaps this item would prove to be a tutorial in the future..
|
|
|
03-14-2005, 05:16 AM
|
#5 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 734
|
I use this simple code in windows
Code:
#include <wininet.h>
#pragma link "lib/wininet.lib"
CString contents;
bool getwebdata(TCHAR *server, unsigned short port, TCHAR *uri) {
HINTERNET hINet, hConnection, hData;
char buffer[256];
DWORD dwRead;
hINet = InternetOpen(_T("Mozilla/1.0"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if ( !hINet ) {
return false;
}
try {
hConnection = InternetConnect(hINet, server, port, _T(" "),_T(" "), INTERNET_SERVICE_HTTP, 0, 0);
if ( !hConnection ) {
InternetCloseHandle(hINet);
return false;
}
// Get data
hData = HttpOpenRequest(hConnection, "GET", uri, NULL, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD, 0);
if ( !hData ) {
InternetCloseHandle(hConnection);
InternetCloseHandle(hINet);
return false;
}
HttpSendRequest( hData, NULL, 0, NULL, 0);
while( InternetReadFile(hData, buffer, 255, &dwRead) ) {
if ( dwRead == 0 ) break;
buffer[dwRead] = 0;
contents += buffer;
}
}
catch( ... ) {
ShowMessage("Error sending/receiving data from internet");
// e->ReportError();
// e->Delete();
}
InternetCloseHandle(hConnection);
InternetCloseHandle(hINet);
InternetCloseHandle(hData);
return true;
}
Donno if it works properly in VS2k3 but it gives you a rough idea how to use the Windows wininet library if your app is for use in Win OS
|
|
|
03-16-2005, 03:00 PM
|
#6 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 14
|
Thats MFC not standard C++ though.
Anyways I got example for Java (not javascript) should you want one too.
|
|
|
03-16-2005, 07:49 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Mar 2005
Posts: 14
|
eh sorry - i thought this was standard c++
|
|
|
03-16-2005, 11:48 PM
|
#8 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
It was in the standard C++ forum, but I moved it.
__________________
|
|
|
03-17-2005, 03:52 AM
|
#9 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 734
|
Quote:
|
Originally Posted by webcomplete.com
Thats MFC not standard C++ though.
|
Quote:
|
Originally Posted by robertfoley
I am new to C++ and am developing on MicroSoft Visual Studio 2003 (Visual C++).
|
It's not MFC specific, since you can use the wininet.dll any way you want.
Shure you can write your own fsockopen() or something but robert says he's a newbie and it does take a lot more work and POSIX works completely different anyway.
So i thought the above code might help him.
|
|
|
03-19-2005, 12:08 AM
|
#10 (permalink)
|
|
Code Monkey
Join Date: Mar 2005
Posts: 56
|
Thanks DJMaze! I finally got something working w/ MSVS 2003.
For some reason, HttpOpenRequest() HttpSendRequest() pair wasn't working, so after poking around the docs found InternetOpenUrl().
The big hurdle for me was not knowing this set of functions even existed, so I now have a whole new world to explore!
BTW, I found libwww on the w3c.org website, finally got their samples to work, and your method is WAY more simple and clean.
Anyone who wants my simple MFC based app based on DJMaze's code snippet is welcome to email me for it.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:51 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|