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