Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to open a file designated with file uri scheme

Reply
Thread Tools

How to open a file designated with file uri scheme

 
 
Jerry Fleming
Guest
Posts: n/a
 
      12-01-2010
Hi,

I was wondering how to access a file designated with file uri scheme,
such as <file:///home/user/a_file.txt>. Of course I can do without the
file:// prefix. Does this scheme require creation of socket, as we do to
access files served on the web (http/ftp, etc)? I am using the
fopen/fread suits as found in stdio.h, if that helps.

Thanks.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-01-2010
Jerry Fleming <(E-Mail Removed)> writes:
> I was wondering how to access a file designated with file uri scheme,
> such as <file:///home/user/a_file.txt>. Of course I can do without the
> file:// prefix. Does this scheme require creation of socket, as we do to
> access files served on the web (http/ftp, etc)? I am using the
> fopen/fread suits as found in stdio.h, if that helps.


The syntax of file names accepted by fopen() is entirely
implementation-defined; the C standard only requires that it's a string.

A C implementation *could* have an fopen() that accepts arbitrary URIs
as file names, and does whatever it needs to do to make that work.

In practice, few if any C implementations do this.

You can probably convert a file: URI to a usable file name, but as "The
China Blue and the Gray" points out this is potentially much more
complicated than just dropping the "file://" prefix.

Another potential problem: I just ran this program:

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *f;
int c;
f = fopen("file:///home/user/a_file.txt", "r");
if (f == NULL) {
exit(EXIT_FAILURE);
}
while ((c = getc(f)) != EOF) {
putchar(c);
}
fclose(f);
return 0;
}

and got this output:

hello, world

The first argument to fopen() isn't a URI, it's just a file name
(I had created a directory named "file:", with subdirectories "home"
and "user", the latter containing a file named "a_file.txt").

Fortunately, there are plenty of libraries that will do this kind
of thing for you. Google "libcurl" for one example. This is
system-specific and outside the scope of the C language standard
(which says nothing about sockets or network support).

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Jerry Fleming
Guest
Posts: n/a
 
      12-02-2010
On 2010-12-02 00:42, Keith Thompson wrote:
> Fortunately, there are plenty of libraries that will do this kind
> of thing for you. Google "libcurl" for one example. This is
> system-specific and outside the scope of the C language standard
> (which says nothing about sockets or network support).
>


Thanks Keith and "The China Blue and the Gray". Your answers are really
enlightening. Actually, I was reading the source code of curl and did a
'grep file: *', expecting to see some prefix-removing lines; when it
turned out to have nothing, I was puzzled. It might take some
abstraction so that the prefix is not hard-coded, or there might be even
a better way. I have to read it carefully.
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      12-02-2010
On Thu, 02 Dec 2010 09:10:33 +0800, Jerry Fleming wrote:

> Actually, I was reading the source code of curl and did a
> 'grep file: *', expecting to see some prefix-removing lines; when it
> turned out to have nothing, I was puzzled. It might take some
> abstraction so that the prefix is not hard-coded, or there might be even
> a better way. I have to read it carefully.


I suspect it will split the URI at the first colon to obtain the scheme
("file", "http", etc).

http://en.wikipedia.org/wiki/URI_scheme#Generic_syntax

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
open-uri 414 Request-URI Too Large Jay 99 Ruby 2 04-04-2009 12:10 PM
Determine whether a URI scheme is enabled Chris Davies Javascript 4 10-05-2007 01:19 PM
java.net.URI.relativize(java.net.URI) not really working Stanimir Stamenkov Java 1 08-17-2005 06:24 PM
Help with error: "Invalid URI: The format of the URI could not be determined." Simon Harris ASP .Net 0 05-10-2005 04:33 PM



Advertisments