Click to See Complete Forum and Search --> : Reading data from an online text file...
_Cobra
January 3rd, 2007, 09:29 PM
What I want to do in a program is read data from a text file. I've got some code that works fine for text files in the solution's folder:
try{
StreamReader^theReader = gcnew StreamReader("file.txt");
}
catch(FileNotFoundException^a){
MessageBox::Show("File Not Found Exception:\r\n" + g, "Alert", MessageBoxButtons::OK);
}
catch(ArgumentException^b){
MessageBox::Show("Argument Exception:\r\n" + f, "Alert", MessageBoxButtons::OK);
}
What if I wanted to read the data in from an online text file? I tried to replace file.txt with the URL of the text file but got the argument exception error.
Am I going at this the wrong way?
Please, go easy on the C++ jargon in any replies. I'm quite new to this.
zerver
January 4th, 2007, 08:49 AM
That looks like C#, you have come to the wrong forum
You can have a look at URLDownloadToFile
http://msdn2.microsoft.com/en-gb/library/ms775123.aspx
Marc G
January 4th, 2007, 09:17 AM
[ moved thread ]
_Cobra
January 4th, 2007, 08:48 PM
That looks like C#, you have come to the wrong forum
You can have a look at URLDownloadToFile
http://msdn2.microsoft.com/en-gb/library/ms775123.aspx
I tried the code from the page you linked me to, but I'm getting an error when I tried using it in my program.
//*****WEBPAGE'S CODE:
HRESULT URLDownloadToFile( LPUNKNOWN pCaller,
LPCTSTR szURL,
LPCTSTR szFileName,
DWORD dwReserved,
LPBINDSTATUSCALLBACK lpfnCB
);
//*****MY CODE:
HRESULT URLDownloadToFile(
NULL,
"http://www.my_webpage.com/filename.txt",
"filename.txt",
0,
NULL);
Did I format this correctly? Whenever I debug, I get an error that I have too many initializers. Any ideas?
While I'm at it, do I have to download this file before I use it in my program? Is there a way to read data in directly from it?
Thanks guys,
-David
Marc G
January 5th, 2007, 04:18 AM
You should be calling it like this:
HRESULT hRes = URLDownloadToFile(
NULL,
"http://www.my_webpage.com/filename.txt",
"filename.txt",
0,
NULL);
_Cobra
January 5th, 2007, 04:23 PM
I tried that but I'm still getting an error on the last line:
error C2664: 'URLDownloadToFileW' : cannot convert parameter 2 from 'const char [39]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Zaccheus
January 5th, 2007, 05:49 PM
If you are writing this in C++/CLI, would it not be better to use System::Net::WebClient::DownloadFile (http://msdn2.microsoft.com/en-us/library/system.net.webclient.downloadfile.aspx) ?
System::Net::WebClient^ webClient = gcnew System::Net::WebClient();
webClient->DownloadFile("http://www.foo.com/bar.txt", "C:\\foo\\bar.txt");
:)
Marc G
January 9th, 2007, 11:27 AM
If you are using C++/CLI, then use Zaccheus solution.
Otherwise, add an L before your strings or put them around _T(), like L"http:..." or _T("http:...")
giddy_guitarist
January 28th, 2007, 01:16 AM
Hi ,
i have the exact same problem ,i'm making a simple program called message(its just a silly program).... should download a message from a .txt file on a URL and display it to the user after a given time.
I'm using PURE C , and win32 API (no C++) and i have vs.net 2005. I went to Project Menu ->Properties -> Configuration Properties|Linker| General and added "D:\Microsoft Visual Studio 8\VC\PlatformSDK\Lib" to Additional Library Directories
i also have
#include <urlmon.h>
i use the URLdownload API in a function like so:
void GetSettings()
{
char temp[2];
URLDownloadToFile(0,"<URL>","C:\\temp.txt" , 0,0);
FILE *fp;
fp = fopen(TMP_PATH ,"r");
fgets(the_message , 200 , fp);
fgets(temp , 2 , fp);
show_at = atoi(temp);
fclose(fp);
}
and the 2 errors i get even after doing the above is:
Error 4 error LNK2019: unresolved external symbol _URLDownloadToFileA@20 referenced in function "void __cdecl GetSettings(void)" (?GetSettings@@YAXXZ) main.obj
Error 5 fatal error LNK1120: 1 unresolved externals e:\Gid\My Creations\Message Prank\Debug\message.exe 1
i also get 2 deprecated warnings for fopen and srcat ..and 1 :
Warning 3 warning LNK4076: invalid incremental status file '.\Debug/message.ilk'; linking nonincrementally message
also , dont know if its worth mentioning , if i hover over URLDownloadToFile() i get a tooltip telling me its defined and could my problem have anything to do with ASCII and unicode.The linker does say: "_URLDownloadToFileA"
Could someone PLEASE HELP!
Gideon
Marc G
January 28th, 2007, 04:25 AM
The error is pretty clear. It is missing the URLDownloadToFile. A quick lookup on MSDN would tell you that you need to link with Urlmon.lib to use this function.
giddy_guitarist
January 31st, 2007, 01:56 AM
ok , i did'nt find anything on msdn but on wikipedia somehow.
The directory has to be specified under Project Menu ->Properties -> Configuration Properties|Linker| General|Additional Lib. Dep.
and then the LIBRARY ITSELF has to be specified at Project Menu ->Properties -> Configuration Properties|Linker|INPUT! | additional dep.
ok so thats solved! but now . . .. . i get :
a debug assertion failed!~!!!!! - fgets.c
this is because the file c:\temp.txt never gets created! . .. URLDownloadTOFile() doesnt! work! ARRRG!
thats how i call it :
void GetSettings()
{
char temp[2];
URLDownloadToFile(0,"http:////gidisrael.googlepages.com//message.txt","c:\\temp.txt", 0,0);
FILE *fp;
fp = fopen("c:\\temp.txt","r");
//if(fp==NULL)........
fgets(the_message , 200 , fp);
fgets(temp , 2 , fp);
show_at = atoi(temp);
fclose(fp);
}
am i missing something somewhere!!!!! =(
plz help!
giddy_guitarist
February 2nd, 2007, 02:25 AM
ok , im sorry. . . the URL to the website is http://gids.. . . .no escape slashes and the path to the FILE . .should have double slashes. C:\\temp.txt
Gideon
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.