C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-19-2007, 06:40 AM   #1
Registered User
 
Join Date: Sep 2006
Posts: 98
Reading text files on the internet

Can anyone tell me how to read a text file or a picture on the internet? For example, I want to compare the data in a string called "data", with a text file, for example, located at http://www.textfiles.com/text.txt. How would I go about downloading the text file into a string? I can do this locally with a file on my computer using:
Code:
string user="C:\stuff\text.txt";
string tmp="";
using (FileStream fs = File.Open(user, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
     byte[] b = new byte[1024];
     UTF8Encoding temp = new UTF8Encoding(true);

      while (fs.Read(b, 0, b.Length) > 0)
      {
            tmp += (temp.GetString(b));
       }
}
Like I said, this works for local files, so how can I modify this to read the file online instead?
Thanks
Mavix is offline   Reply With Quote
Old 06-19-2007, 07:08 AM   #2
Registered User
 
Join Date: Sep 2006
Posts: 98
I just read on another forum that I would have to download the file first and then access it locally like I do now. Does anyone know how I can download the file?
Mavix is offline   Reply With Quote
Old 06-19-2007, 08:06 AM   #3
and the Hat of Clumsiness
 
GanglyLamb's Avatar
 
Join Date: Oct 2002
Posts: 1,101
The power of Google.

First item: http://www.primaryobjects.com/CMS/Article64.aspx

I havent looked at this article in detail but it will definately help you achieve your goal.
GanglyLamb is offline   Reply With Quote
Old 06-19-2007, 08:07 AM   #4
Registered User
 
Join Date: Sep 2006
Posts: 98
Well, I figured out how to do it. I used
Code:
string url="http://www.textfiles.com/text.txt";
WebRequest request = WebRequest.Create(url);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string data = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
It works, but is a bit slow. My second question is: How do change the code to write the data to the file instead of reading it?
Mavix is offline   Reply With Quote
Old 06-19-2007, 08:12 AM   #5
Registered User
 
AtomRiot's Avatar
 
Join Date: Jan 2003
Posts: 120
did you try to read it in and store it, then alter it, then use StreamWriter to write it back?
__________________
All Your Base Are Still Belong to Someone!!!
And you Remember that!!!
AtomRiot is offline   Reply With Quote
Old 06-19-2007, 08:29 AM   #6
Registered User
 
Join Date: Sep 2006
Posts: 98
I have now, but I don't know how to do that. Could you perhaps give me an example of how to do it?
Mavix is offline   Reply With Quote
Old 06-19-2007, 10:19 AM   #7
Registered User
 
AtomRiot's Avatar
 
Join Date: Jan 2003
Posts: 120
well chances are you will have to upload it via some http since most web sites do not have direct write access to the files you can view.
__________________
All Your Base Are Still Belong to Someone!!!
And you Remember that!!!
AtomRiot is offline   Reply With Quote
Old 06-19-2007, 11:03 AM   #8
Registered User
 
AtomRiot's Avatar
 
Join Date: Jan 2003
Posts: 120
for going the ftp route

http://www.madskristensen.dk/blog/Si...d+In+C+20.aspx
__________________
All Your Base Are Still Belong to Someone!!!
And you Remember that!!!
AtomRiot is offline   Reply With Quote
Old 06-20-2007, 05:38 AM   #9
Registered User
 
Join Date: Sep 2006
Posts: 98
Thanks for the link! How can I upload using http?
Mavix is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use FTP? maxorator C++ Programming 8 11-04-2005 03:17 PM
reading text files stimpyzu C++ Programming 11 04-17-2004 07:45 AM
Reading binary files and writing as text thenrkst C++ Programming 8 03-13-2003 10:47 PM
Reading spaces, carrage returns, eof from text files thenrkst C++ Programming 1 03-11-2003 05:18 AM
reading certain parts of text files Captain Penguin C++ Programming 2 10-10-2002 09:45 AM


All times are GMT -6. The time now is 09:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22