Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-09-2005, 07:14 PM   #1 (permalink)
robertfoley
Registered User
 
Join Date: Mar 2005
Posts: 1
robertfoley is on a distinguished road
Unhappy 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
robertfoley is offline   Reply With Quote
Old 03-09-2005, 10:17 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 03-13-2005, 08:15 PM   #3 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
I would like to learn how to do this also. Is there a tutorial or sample code that shows how to do this? Thanks.
QUantumAnenome is offline   Reply With Quote
Old 03-14-2005, 03:39 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,696
redhead is on a distinguished road
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..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 03-14-2005, 04:16 AM   #5 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 661
DJMaze is on a distinguished road
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
DJMaze is offline   Reply With Quote
Old 03-16-2005, 02:00 PM   #6 (permalink)
webcomplete.com
Registered User
 
Join Date: Mar 2005
Posts: 14
webcomplete.com is on a distinguished road
Thats MFC not standard C++ though.

Anyways I got example for Java (not javascript) should you want one too.
webcomplete.com is offline   Reply With Quote
Old 03-16-2005, 06:49 PM   #7 (permalink)
webcomplete.com
Registered User
 
Join Date: Mar 2005
Posts: 14
webcomplete.com is on a distinguished road
eh sorry - i thought this was standard c++
webcomplete.com is offline   Reply With Quote
Old 03-16-2005, 10:48 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
It was in the standard C++ forum, but I moved it.
__________________
Valmont is offline   Reply With Quote
Old 03-17-2005, 02:52 AM   #9 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 661
DJMaze is on a distinguished road
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.
DJMaze is offline   Reply With Quote
Old 03-18-2005, 11:08 PM   #10 (permalink)
QUantumAnenome
Code Monkey
 
Join Date: Mar 2005
Posts: 55
QUantumAnenome is on a distinguished road
Send a message via Yahoo to QUantumAnenome
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.
QUantumAnenome is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assembelly code register addressing toblerone Assembly 5 05-22-2006 02:53 AM
download a remote file to the local machine. sde Java 4 12-04-2004 10:49 AM


All times are GMT -8. The time now is 07:36 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting