473,386 Members | 1,830 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,386 software developers and data experts.

How to read an input text from keyboard by the std. lang. C?

Hello,

how to read an input text from keyboard (on a console) by the standard
languace C, please? I really need a tip about this question.

TIA, Vinicius.
Nov 14 '05 #1
7 2657
Ren
What kind of input? Do you want to read a word? A sentence? A number?
There's more than one way to get input from stdin, but for the most
part you'll find yourself using fgets:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char buffer[BUFSIZ];

printf("Enter some text: ");
fflush(stdout);
if (fgets(buffer, sizeof buffer, stdin) == NULL) {
fprintf(stderr, "Input error\n");
return EXIT_FAILURE;
}
printf("%s\n", buffer);

return EXIT_SUCCESS;
}

-Ren

Nov 14 '05 #2
Vinicius wrote:
Hello,

how to read an input text from keyboard (on a console) by the standard
languace C, please? I really need a tip about this question.

TIA, Vinicius.


No, you need a clue. A tip would be getchar(). You have a chance of
getting a clue if you read a decent C reference book.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #3
Ever thought of using scanf? What exactly do u want to take as input?
character?string? digit?

dont use "gets" by any way. ur code will crash.

Nov 14 '05 #4
On Sat, 11 Dec 2004 19:56:53 -0500, "Afifov" <af****@yahoo.com> wrote:
Ever thought of using scanf? What exactly do u want to take as input?
character?string? digit?
Using scanf by itself can be bad advice, since it leaves unparsed input
available for the next input routine. This wouldn't be a problem for some
people, but has been known to cause infinite loops in programs written by
newcomers.
dont use "gets" by any way. ur code will crash.


gets() will only crash (or otherwise cause undefined behaviour) if the
text entered exceeds the length of the buffer provided. If you want to
discourage the use of gets(), you should point to fgets() as a suitable
substitute. (In addition, you can also recommend use of the
fgets()/sscanf() combination for parsing input line-by-line.)

Nov 14 '05 #5
Raymond Martineau wrote:
.... snip ...
dont use "gets" by any way. ur code will crash.


gets() will only crash (or otherwise cause undefined behaviour) if
the text entered exceeds the length of the buffer provided. If you
want to discourage the use of gets(), you should point to fgets()
as a suitable substitute. (In addition, you can also recommend
use of the fgets()/sscanf() combination for parsing input
line-by-line.)


Or use the completely safe ggets, with the simplicity of gets, the
safety of fgets, and no worries about input line length. See it
at:

<http://cbfalconer.home.att.net/download/ggets.zip>

--
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 #6
what does sscanf do?

Nov 14 '05 #7
On Tue, 14 Dec 2004 07:47:04 -0500, Afifov wrote:
what does sscanf do?


The same as scanf() and fscanf() except that it reads its input from a
string rather than a file. Any good C reference should explain it.

Lawrence

Nov 14 '05 #8

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

Similar topics

21
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
13
by: j. del | last post by:
I am just beginning to write programs... and my first task that I have set myself is to write a little program that will generate cryptic bywords from a source of text. A cryptic byword is...
14
by: Mark | last post by:
Hi, At the top of my php file I have got :- <body onblur="self.focus();"> so when I click off onto another window, my window comes back up in front which is perfect. The problem however, is...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
1
by: Csaba Gabor | last post by:
Short version: if the user types an alt+ctrl+char combination which leads to a defined character, but s/he's not in a input(text)/textarea, then I'd like that keystroke combination to do the same...
2
by: Rajen | last post by:
Suppose the field length is 25 characters. After entering the 25th character, it should be available to process. Program should not wait for the user to press enter/return key. Thank you.
5
by: Rajkiran R.B. | last post by:
Well I have multiple keyboard layouts installed in my system.. I have written a program in C# such that the required input language can be selected using the following piece of code ...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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,...
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...

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.