473,407 Members | 2,629 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,407 software developers and data experts.

Issues with input

is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...
Nov 14 '05 #1
8 1151
"Spartan815" <ab************@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
is there another function or way of going about getting input from the
command line besides getch().
From the command line? Wazzat? If you meant "standard input", there is a
multitude of standard functions:

[f]getc()
getchar()
fgets()
[f]scanf() /* better not */
gets() /* DEFINITELY not! */

On the other hand, there is no getch().
Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display.
s/display/displayed

I am not sure I understand what you are trying to do. To save N lines
of text you need an array of N strings. You could do it dynamically
with a linked list. They are dead easy to use as a stack, an ideal
structure for keeping a history.
I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...


This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading".

Peter
Nov 14 '05 #2
Spartan815 wrote:

is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...


I suspect you mean the terminal, or stdin, rather than command
line. Standard routines include getc, fgetc, fgets. scanf is not
recommended for this purpose, nor is gets. However you can also
use my favorite, ggets, available on my site, download section.
R. Heathfield has another similar routine, that requires more care
and feeding IMO.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3

"Spartan815" <ab************@hotmail.com> wrote in message

is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple
while loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better
question is can anyone point me to some good C resources online so
I can actually read about it...

getc() is the best function for reading from stdin. gets() is unsafe,
scanf() is difficult to use, fgets() has subtle problems on overflow.

So write this function

char *getline(void)

calls malloc() and getc() to return a line from stdin.

Now all you need is an array of character points

char *history[10];
int current = 0; /* this is not strictly needed but may make things easier
to understand */

Intitialise all elements of history to NULL. Then for the first ten lines
read in the lines, incrementing current. When current gets to ten, the array
is full. Therefore free the top entry (history[0]) and call memmove() to
move all the pointers up one place. Then add your newly read line to the
bottom.
Nov 14 '05 #4
ma*****@55bank.freeserve.co.uk says...
"Spartan815" <ab************@hotmail.com> wrote in message
is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple
while loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better
question is can anyone point me to some good C resources online so
I can actually read about it...
getc() is the best function for reading from stdin. gets() is unsafe,
scanf() is difficult to use, fgets() has subtle problems on overflow.


I'll second all that. But scanf has the additional problem of necessarily
parsing your input in ways that don't allow you to definitively recover the raw
input.

I have a webpage explaining all of this, with sample code, here:

http://www.pobox.com/~qed/userInput.html
So write this function

char *getline(void)

calls malloc() and getc() to return a line from stdin.

Now all you need is an array of character points

char *history[10];
int current = 0; /* this is not strictly needed but may make things easier
to understand */

Intitialise all elements of history to NULL. Then for the first ten lines
read in the lines, incrementing current. When current gets to ten, the array
is full. Therefore free the top entry (history[0]) and call memmove() to
move all the pointers up one place. Then add your newly read line to the
bottom.


That's one way, but why not just retain current as a modulo 10 value rather
than shifting your whole history? For example, using the code I indicated on
the webpage above, we can write simply:

char * history[10] = {NULL, ..., NULL};
int current = 0;

while (...) {
free (history[current % 10U]); /* free(NULL) is ok */
getstralloc (&(history[current % 10U]));
current++;

...

/* Dump the history */
for (i=current-10; i < current; i++) {
if (history[i % 10U]) {
printf ("%d) %s\n", i, history[i % 10U]);
}
}
}

There are ways to dramatically reduce the cost of the "%" operations, but I
will leave that as an exercise to the reader.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 14 '05 #5
> "This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading"."


-- Wow, way to be a total jerk about it. It was a simple question,
you chould have decided not to post a response at all (and in this
case it probably would have been better). Not that I should even
dignify what you said with a response but here we go...

1) I dont have a car to get to the library
2) I dont have money to go throwing around and buying books beyond the
required text books for classes
3) the text for the class is a book on operating systems -- with
little or no examples of code

and for the above reasons, I was looking for an online resource so I
could do that "reading" thing you talked about from home. You know,
here on my computer, they have those things called webpages that often
include helpful information on a wide range of topics.
Nov 14 '05 #6
"Spartan815" <ab************@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
"This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading"."

Please keep the attributions for the benefit of others.
-- Wow, way to be a total jerk about it.
Indeed. I've just had an unhappy love affair, so I don't see why anybody
else should have a good time ;-)
It was a simple question,
Hmm, let's see:
"is there another function or way of going about getting input from the
command line besides getch()."

First of all, it makes no sense. There is no command line and no getch()
in standard C. You got two independent answers nevertheless, all in good
intentions.
you chould have decided not to post a response at all (and in this
case it probably would have been better).
As you wish. I will not bother the next time. But you should get used to
some level of sarcasm on this newsgroup, if you want to learn something.
1) I dont have a car to get to the library
A bicycle or even your own feet would do.
2) I dont have money to go throwing around and buying books beyond the
required text books for classes
"Throwing around?" Now this must be the most arrogant response I've seen
in years. Knowledge is money, have you heard that? And knowledge /costs/
money too. If you are not willing to invest in your own education, then
do not be surprised if you don't achieve much later.
3) the text for the class is a book on operating systems -- with
little or no examples of code
Then get a second book. The FAQ has some recommendations. It does not
hurt to have books on several topics.
and for the above reasons, I was looking for an online resource so I
could do that "reading" thing you talked about from home. You know,
here on my computer, they have those things called webpages that often
include helpful information on a wide range of topics.


That's all very nice. Howevere, a book is better on many accounts. First,
it is much more readily available. Second, you could make notes in it.
Then there is the thing about reading in bed - if you have a laptop with
wireless internet connection, then you surely have enough money to afford
a book too. And even a laptop would not be much help for reading on the
bus on the way to work or school.

I write all this with the intention to help you. If you want to take
offence, that is your choice. Message ends.

Peter
Nov 14 '05 #7

"Paul Hsieh" <qe*@pobox.com> wrote in message

That's one way, but why not just retain current as a modulo 10 value > rather than shifting your whole history?

As it is we only have ten pointers to move, in the context of user input.
Even an extremely slow computer will do this in under just-noticeable time,
and it keeps things simple.
However generally a circular buffer is a faster way of storing a fixed size
list. If we had many thousands of entries the OP might like to consider
doing this.

Nov 14 '05 #8
Spartan815 <ab************@hotmail.com> scribbled the following:
"This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading"."
-- Wow, way to be a total jerk about it. It was a simple question,
you chould have decided not to post a response at all (and in this
case it probably would have been better).


*PLONK*

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #9

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

Similar topics

4
by: Scott Navarre | last post by:
Hi, I have Red Hat 8.0 and have the default Mozilla browser that comes with it. I am programming in javascript and have come across something problematic. Given the following code: <HTML>...
5
by: Zambien | last post by:
Hi all, Here's my problem. I have tables that are using the menu/submenu idea for hiding rows. This works fine in IE (of course) and does show/hide correctly in netscape, but as soon as the...
2
by: santiago | last post by:
hi I'm new to javascript, and I've been looking around for some references on compatibility issues, but didn't found much. I'm intrested in any and all kind of recommandations about which...
16
by: Justin Lazanowski | last post by:
Cross posting this question on the recommendation of an I have a .NET application that I am developing in C# I am loading information in from a dataset, and then pushing the dataset to a grid,...
3
by: Newsscanner | last post by:
Hello, Everything is beginning to work, but there are still a few issues left. Please forgive my ignorance and hence stupid questions (Newbie). I can now enter data with no problem, I get no...
7
by: David Laub | last post by:
I have stumbled across various Netscape issues, none of which appear to be solvable by tweaking the clientTarget or targetSchema properties. At this point, I'm not even interested in "solving"...
8
by: GaryDean | last post by:
In an 2.0 asp app I used vs.net 2005 to create a TableAdapter:Dataset in my App_code directory. I also created a new vb class in that same directory. I have two issues: 1. I notice that there...
1
by: John Fly | last post by:
I've used standard functions like find_first_of and find_first_not_of for some time. I was about to implement a quick string tokenizer similar to the example below when I ran across an old thread...
5
by: jej1216 | last post by:
I am writing a PHP page to open a row of data from a MySQL database so the user can edit the data. This page mimics the inital data entry page. Everything is working so far, with the following two...
2
by: George Sakkis | last post by:
I'm trying to use codecs.open() and I see two issues when I pass encoding='utf8': 1) Newlines are hardcoded to LINEFEED (ascii 10) instead of the platform-specific byte(s). import codecs f =...
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.