473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Advanced reading from stdin with standard C

[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

Nov 12 '07 #1
3 1900
On Nov 12, 10:25 am, Joakim Hove <joakim.h...@gm ail.comwrote:
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.

Nov 12 '07 #2
Joakim Hove <jo*********@gm ail.comwrites:
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.progr ammer might help.</off-topic>

--
Ben.
Nov 12 '07 #3
In article <11************ **********@v3g2 000hsg.googlegr oups.com>,
Joakim Hove <jo*********@gm ail.comwrote 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 .programmeror
<news:comp.os.m s-windows.program mer.win32>.

Nov 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5830
by: Bernhard Kuemel | last post by:
Hi! I want to read/write commands and program input to/from /bin/bash several times before I close the stdin pipe. However, reading from cat hangs unless I first close the stdin pipe. <?php $descriptorspec = array( 0 => array("pipe", "r"), // stdin is a pipe that the child
1
20427
by: Scott Shaw | last post by:
Hi all, I was wondering if you could help out with this problem that I am having. What I am trying to do is detect keyboard input in a while loop without halting/pausing the loop until the key is pressed (without hitting return). I looked at serveral faq's on the net and installed the cspan readkey module and neither seems to work most likey...
23
7685
by: herrcho | last post by:
What's the difference between STDIN and Keyboard buffer ? when i get char through scanf, i type in some characters and press enter, then, where do the characters go ? to STDIN or Keyboard buffer ? are they same ? thanks ^^
3
10140
by: Fernando Arbeiza | last post by:
Hi: I need some clarification about a code like this: printf("%s", "a string with NO trailing newline"); scanf("%d", &i); Regarding if a fflush() of the standard output is needed or not. I think the relevant portions of the standard are:
8
3889
by: orium69 | last post by:
Hi! how could i know if it was written something in stdin since the last time i read it? Tks...
2
5216
by: Bernard Liang | last post by:
In response, I have another question about the scanf family. After reading in a %d value, for instance, do they immediately wade through all subsequent whitespace until a non-whitespace character is reached, and then continue analysing the format string? For instance, suppose I have a buffer int num; char chr1, chr2, chr3, chr4; char...
0
1331
by: Miha | last post by:
I wrote a simple Windows Application based application in C# called the CallWebServiceGui. This application is started from a different application called WsGuiStarter. The WsGuiStarter redirects the standard input from the CallWebServiceGui as a simple method for inter-process communication (IPC) between the two applications. For the reason...
111
19960
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- http://www.riscossione.info/
5
705
by: Luis Zarrabeitia | last post by:
I have a problem with this piece of code: ==== import sys for line in sys.stdin: print "You said!", line ==== Namely, it seems that the stdin buffers the input, so there is no reply until a huge amount of text has bin written. The iterator returned by xreadlines
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.