How to open a file designated with file uri scheme

J

Jerry Fleming

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.
 
K

Keith Thompson

Jerry Fleming said:
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).
 
J

Jerry Fleming

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.
 
N

Nobody

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top