Advanced reading from stdin with standard C

Discussion in 'C Programming' started by Joakim Hove, Nov 12, 2007.

  1. Joakim Hove

    Joakim Hove Guest

    [I am sorry if this post is a contradiction in terms - i.e. if there
    is no such thing as stdio in the C standard?]

    Hello,

    I am reading a filename from the user - the loop is typically like
    this:

    1. Read a directory from the user.
    2. Read several filenames from the user.

    The filenames read in 2. above will typically (but not necessarily) be
    located in the directory entered under point 1., so to make it more
    streamlined for the user I would like something like

    Give name of file with.. => /some/path/given/first/_

    I.e. the path /some/path/given/first/ should be printed first, and
    then user can continue editing. I have the following two requirements:

    1. When the user has completed the input buffer should contain the
    full path to some file. [This I can manage by initalising the input
    buffer.]
    2. If some file is located in another location, the user should be
    able to delete parts of the path, so the final result can be .e.g. /
    some/other/path/file.txt

    I have looked into gnu readline - but it does not seem it provides
    this functionality. Any tips gretly appreciated.

    Joakim Hove
    Joakim Hove, Nov 12, 2007
    #1
    1. Advertising

  2. On Nov 12, 10:25 am, Joakim Hove <> wrote:
    > 2. If some file is located in another location, the user should be
    > able to delete parts of the path, so the final result can be .e.g. /
    > some/other/path/file.txt


    That's outside the bounds of C and in the realm of whatever program
    handles input before it's sent to your program. To do what you want
    would require a non-portable solution that gives you more control over
    console input. As it is, you only get to see the final result (after
    any edits are made) that's returned by fgets or whatnot.
    Julienne Walker, Nov 12, 2007
    #2
    1. Advertising

  3. In article <>,
    Joakim Hove <> wrote:
    >I.e. the path /some/path/given/first/ should be printed first, and
    >then user can continue editing. I have the following two requirements:


    >1. When the user has completed the input buffer should contain the
    >full path to some file. [This I can manage by initalising the input
    >buffer.]


    No you cannot manage that by initializing the input buffer.
    It contradicts your point 2, that the user should be able to edit
    the directory path: if the user can edit the directory path, they
    can edit right back to the beginning, erasing even the leading
    directory delimeter, leaving only an unqualified (no directory)
    filename.

    >2. If some file is located in another location, the user should be
    >able to delete parts of the path, so the final result can be .e.g. /
    >some/other/path/file.txt


    >I have looked into gnu readline - but it does not seem it provides
    >this functionality. Any tips gretly appreciated.


    I haven't used gnu readline before, but a quick check of the
    documentation suggests to me that you could call rl_insert_text()
    to insert the directory path before allowing the user to do any
    editting.
    http://tiswww.case.edu/php/chet/readline/readline.html#SEC36

    But you should check that with a newsgroup or mailing list
    that deals with gnu readline. If you do use gnu readline, be sure
    to read the licensing terms first.

    What you are asking for is not possible using only the facilities
    provided by the C I/O library.
    --
    "Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us." -- Ecclesiastes
    Walter Roberson, Nov 12, 2007
    #3
  4. Joakim Hove <> writes:

    > I am reading a filename from the user - the loop is typically like
    > this:
    >
    > 1. Read a directory from the user.
    > 2. Read several filenames from the user.
    >
    > The filenames read in 2. above will typically (but not necessarily) be
    > located in the directory entered under point 1., so to make it more
    > streamlined for the user I would like something like
    >
    > Give name of file with.. => /some/path/given/first/_
    >
    > I.e. the path /some/path/given/first/ should be printed first, and
    > then user can continue editing. I have the following two requirements:
    >
    > 1. When the user has completed the input buffer should contain the
    > full path to some file. [This I can manage by initalising the input
    > buffer.]
    > 2. If some file is located in another location, the user should be
    > able to delete parts of the path, so the final result can be .e.g. /
    > some/other/path/file.txt
    >
    > I have looked into gnu readline - but it does not seem it provides
    > this functionality. Any tips gretly appreciated.


    <Off-topic>I think you'll find GNU readline will do just fine -- at
    least I can do what you seem to want in a few lines.

    A post in comp.unix.programmer might help.</off-topic>

    --
    Ben.
    Ben Bacarisse, Nov 12, 2007
    #4
  5. Joakim Hove

    santosh Guest

    In article <>,
    Joakim Hove <> wrote on Monday 12 Nov 2007 8:55
    pm:

    > [I am sorry if this post is a contradiction in terms - i.e. if there
    > is no such thing as stdio in the C standard?]


    Standard C does specify I/O functions collected under a header called
    stdio.h.

    > I am reading a filename from the user - the loop is typically like
    > this:
    >
    > 1. Read a directory from the user.


    Standard C has no support for directories. But POSIX has standardised
    functions for accessing directories.

    > 2. Read several filenames from the user.
    >
    > The filenames read in 2. above will typically (but not necessarily) be
    > located in the directory entered under point 1., so to make it more
    > streamlined for the user I would like something like
    >
    > Give name of file with.. => /some/path/given/first/_
    >
    > I.e. the path /some/path/given/first/ should be printed first, and
    > then user can continue editing. I have the following two requirements:


    If I understand you correctly, you want to print out the path and then
    let the user type the filename just after it and also edit the path
    string if they so desire.

    Standard C has no support for command line editing. This is usually
    provided by your operating system's terminal code. Alternatively you
    might consider the reasonably portable curses library. Versions exist
    for most major systems.

    > 1. When the user has completed the input buffer should contain the
    > full path to some file. [This I can manage by initalising the input
    > buffer.]


    Just print out the path name and tell the user to enter the filename on
    the following line. If they want to specify another path then they can
    enter a full pathname instead of just a filename. You can then read in
    the line and if a directory separator is present then you know that a
    new path has been specified. Otherwise you can just concatenate your
    preselected path with the filename input.

    This method is portable and is not of much inconvenience to users.

    > 2. If some file is located in another location, the user should be
    > able to delete parts of the path, so the final result can be .e.g. /
    > some/other/path/file.txt


    Standard C does not guarantee this. Your system might however provide
    this capability, but you'll likely have to use low-level terminal
    functions.

    > I have looked into gnu readline - but it does not seem it provides
    > this functionality. Any tips gretly appreciated.


    IMO losing portability for a small cosmetic UI feature is not worth it.
    However YMMV. For more details post to a system specific group like
    <news:comp.unix.programmer> or
    <news:comp.os.ms-windows.programmer.win32>.
    santosh, Nov 12, 2007
    #5
    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. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,811
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    763
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,319
    jacob navia
    Aug 29, 2009
  4. Michele Simionato
    Replies:
    1
    Views:
    583
    Lacrima
    Mar 27, 2010
  5. Stefano Sabatini
    Replies:
    6
    Views:
    280
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page