Thread: Reading text files on the internet

  1. #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

  2. #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?

  3. #3
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    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.

  4. #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?

  5. #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!!!

  6. #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?

  7. #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!!!

  8. #8
    Registered User AtomRiot's Avatar
    Join Date
    Jan 2003
    Posts
    120
    All Your Base Are Still Belong to Someone!!!
    And you Remember that!!!

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Thanks for the link! How can I upload using http?

Popular pages Recent additions subscribe to a feed

Similar Threads

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