Advanced reading from stdin with standard C

J

Joakim Hove

[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
 
J

Julienne Walker

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

Walter Roberson

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

Ben Bacarisse

Joakim Hove said:
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>
 
S

santosh

[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
<or
<news:comp.os.ms-windows.programmer.win32>.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top