How to open a file designated with file uri scheme

Discussion in 'C Programming' started by Jerry Fleming, Dec 1, 2010.

  1. 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.
    Jerry Fleming, Dec 1, 2010
    #1
    1. Advertising

  2. Jerry Fleming <> 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.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Dec 1, 2010
    #2
    1. Advertising

  3. 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.
    Jerry Fleming, Dec 2, 2010
    #3
  4. Jerry Fleming

    Nobody Guest

    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
    Nobody, Dec 2, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roland Dreier

    Designated struct initializers with Visual C++

    Roland Dreier, Sep 14, 2003, in forum: C Programming
    Replies:
    3
    Views:
    1,446
    Serve La
    Sep 14, 2003
  2. Ark
    Replies:
    1
    Views:
    315
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Sep 14, 2006
  3. ericnoonan
    Replies:
    0
    Views:
    414
    ericnoonan
    Nov 10, 2009
  4. Jay 99
    Replies:
    2
    Views:
    184
    Jay 99
    Apr 4, 2009
  5. Chris Davies

    Determine whether a URI scheme is enabled

    Chris Davies, Oct 3, 2007, in forum: Javascript
    Replies:
    4
    Views:
    99
    Thomas 'PointedEars' Lahn
    Oct 5, 2007
Loading...

Share This Page