Thread Rating:
  • 448 Vote(s) - 2.73 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Files over HTTP1:Files over HTTP
06-15-2011, 04:30 PM, (This post was last modified: 06-16-2011, 10:43 AM by kino.)
Files over HTTP1:Files over HTTP
File Access
It is common for programs to read text from and write text to local or remote data files. An applet may need to read data from a Web page, or write data to the local machine for use the next time the program is run.

Reading from and writing to resources are generally accomplished the same way. A resource is identified, a stream is opened to it, the data is transferred, and the resource is closed. The resource is typically identified using a Url object. This Curl Cue will demonstrate how to read file resources. The examples will use text files obtained via HTTP, but it is worth noting that file access is the same whether the file is local or obtained over HTTP.

An input stream object provides the methods necessary to transfer the data. There are general classes and methods that provide the essential means for reading and writing data, but there are also high-level, convenience procedures that simplify many tasks.

The easiest way to read data from a resource is to treat the resource as a text file. Many resources, such as configuration files and Web pages, are commonly handled this way. In all textual cases, you will have to:

1.Identify the Url

2.Create a TextInputStream to read the data

3.Read and store the data

4.Close the TextInputStream

Reading File Contents
So, how do you read the contents of a file? For our example, we will access the business.txt file that has the following content:

Local Businesses


Convenience Store

Coffee Shop

Print Shop


Fitness Center


Photo Shop


Shoe Store

The easiest way to read the entire contents of a file is to use the read-from procedure. This procedure is actually a utility procedure that combines several operations:

1.It automatically opens the given url as a TextInputStream

2.It reads all the characters out of it into a StringBuf

3.It returns the StringBuf.

{curl 6.0, 7.0 applet}

{pre {read-from {url "business.txt"}}}

The pre text format displays the paragraph contents in a monospace font, preserving whitespace and newlines.

Once the file is read, the program can manipulate the StringBuf, or in the case above, we just displayed the content using pre. You may need to convert the content to a String for further processing, for example: {{read-from {url "business.txt"}}.to-String}.

Reading Line by Line
One of the most common ways to read text from a file is line by line. Most text files will have multiple lines to read, so this approach makes sense, especially if a line needs to be processed by the applet before the next line is read.

The next example demonstrates a simple way to read a text file line by line.
{curl 6.0, 7.0 applet}

   || Create a StringBuf to hold the data
   let lines:StringArray = {StringArray}
   || Identify the url
   let location:Url = {url "business.txt"}

   {with-open-streams in = {read-open location} do
       {until in.end-of-stream? do
           {lines.append {{}.to-String}}

   || Display the stored information
   let display:VBox =  
           background = "beige",
           border-style = "sunken",
           border-width = 2pt,
           border-color = "navy",
           margin = 4pt
   {for line key k in lines do
       {if k==0 then
           {display.add {bold {underline {value line}}}}
           {display.add line}
We use with-open-streams to read each line of a given file and store it in a StringArray. Notice that we detect the end of the file using the end-of-stream? accessor.

{with-open-streams in = {read-open location} do
       {until in.end-of-stream? do
           {lines.append {{}.to-String{font}
For the display VBox content, we looped through the lines StringArray. The loop is organized so that the first line will be displayed underlined and bolded.

{for line key k in lines do
       {if k==0 then
           {display.add {bold {underline{value line{font}{font}
           {display.add line}
In this Curl Cue, we identified two main ways to read file content. The Curl language has many stream classes that can be used based on your application needs. These include:

•Text and binary streams

•Buffered and unbuffered streams

•Seekable and nonseekable streams

For more information regarding reading file content, please refer to the following section in the Curl Documentation:Curl Developer's Guide > File Systems and Networking > Accessing Files and Other Resources


Possibly Related Threads...
Thread Author Replies Views Last Post
  Persistent Data2:HTTP Cookies kino 0 6,502 06-15-2011, 05:27 PM
Last Post: kino
  Forms3:HTTP Forms kino 0 5,036 06-15-2011, 05:02 PM
Last Post: kino
  Files over HTTP4:Viewing RecordSet Data kino 0 4,859 06-15-2011, 04:46 PM
Last Post: kino
  Files over HTTP3:Connected Data kino 0 7,072 06-15-2011, 04:42 PM
Last Post: kino
  Files over HTTP2:Organizing Data using RecordSets kino 0 5,492 06-15-2011, 04:35 PM
Last Post: kino
Forum Jump:

Users browsing this thread:
1 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
INSERT INTO mybb_threadviews (tid) VALUES('16')