473,382 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1881
On Nov 12, 10:25 am, Joakim Hove <joakim.h...@gmail.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*********@gmail.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.programmer might help.</off-topic>

--
Ben.
Nov 12 '07 #3
In article <11**********************@v3g2000hsg.googlegroups. com>,
Joakim Hove <jo*********@gmail.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.ms-windows.programmer.win32>.

Nov 12 '07 #4

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

Similar topics

0
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...
1
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...
23
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...
3
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...
8
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
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...
0
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...
111
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 -- ------------------------------------- ...
5
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 ...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.