Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Advanced reading from stdin with standard C

Reply
Thread Tools

Advanced reading from stdin with standard C

 
 
Joakim Hove
Guest
Posts: n/a
 
      11-12-2007
[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

 
Reply With Quote
 
 
 
 
Julienne Walker
Guest
Posts: n/a
 
      11-12-2007
On Nov 12, 10:25 am, Joakim Hove <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      11-12-2007
In article <(E-Mail Removed). com>,
Joakim Hove <(E-Mail Removed)> 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/read...ine.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
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-12-2007
Joakim Hove <(E-Mail Removed)> 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.
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      11-12-2007
In article <(E-Mail Removed). com>,
Joakim Hove <(E-Mail Removed)> 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>.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
peek at stdin, flush stdin Johnathan Doe C Programming 5 05-17-2013 04:30 PM
How to pass stdin of a C++ program to the stdin of a process createdwith ShellExecute() Ben C Programming 2 08-29-2009 09:47 PM
STDIN, OUT, ERR and $stdin, out, err - Differences? Terry Cooper Ruby 7 06-09-2009 05:48 AM
Reading from stdin then launching a program that reads from stdin strange behaviour Stefano Sabatini Perl Misc 6 07-29-2007 10:38 PM
Reading stdin once confuses second stdin read Charlie Zender C Programming 6 06-21-2004 01:39 PM



Advertisments