reading a single character in C


 
Thread Tools Search this Thread
Top Forums Programming reading a single character in C
# 1  
Old 01-24-2005
reading a single character in C

Can anyone help me?????



My problem is that i want to read only one charcter from keyboard. Each time my program waits to press enter or ^d. I don't want that. As soon as i press a charcter it should proceed to next statement in program without pressing enter or ^d.



please help me..........
# 2  
Old 01-24-2005
Try getchar(3).
# 3  
Old 01-24-2005
There is more to this than just getchar(). The terminal driver must be put into character mode and buffering must be disabled. See this post.
# 4  
Old 01-28-2005
#include <stdio.h>
/* character input */
int fgetc(FILE *stream);
int getc(FILE *stream);
int getchar(void);
int ungetc(int c, FILE *stream);

These read an unsigned char from the input stream where specified, or otherwise stdin. In each case, the next character is obtained from the input stream. It is treated as an unsigned char and converted to an int, which is the return value. On End of File, the constant EOF is returned, and the end-of-file indicator is set for the associated stream. On error, EOF is returned, and the error indicator is set for the associated stream. Successive calls will obtain characters sequentially. The functions, if implemented as macros, may evaluate their stream argument more than once, so do not use side effects here.

There is also the supporting ungetc routine, which is used to push back a character on to a stream, causing it to become the next character to be read. This is not an output operation and can never cause the external contents of a file to be changed. A fflush, fseek, or rewind operation on the stream between the pushback and the read will cause the pushback to be forgotten. Only one character of pushback is guaranteed, and attempts to pushback EOF are ignored. In every case, pushing back a number of characters then reading or discarding them leaves the file position indicator unchanged. The file position indicator is decremented by every successful call to ungetc for a binary stream, but unspecified for a text stream, or a binary stream which is positioned at the beginning of the file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to repeat a character in a field if it's a single character?

I have a csv dataset like this : C,rs18768 G,rs13785 GA,rs1065 G,rs1801279 T,rs9274407 A,rs730012 I'm thinking of use like awk, sed to covert the dataset to this format: (if it's two character, then keep the same) CC,rs18768 GG,rs13785 GA,rs1065 GG,rs1801279 TT,rs9274407... (7 Replies)
Discussion started by: nengcheng
7 Replies

2. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

3. Shell Programming and Scripting

Reading of variable in a single line command

Hi All, Below is a sample command that I can run without any problem in the command line. Command Line dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt -rw-r--r-- 1 monuser oinstall 0 Jan 18 11:02 ./filename_20130118.txt But once I put that command line in file (list.txt) and... (3 Replies)
Discussion started by: padi
3 Replies

4. Shell Programming and Scripting

Problem with character by character reading

Hi friend, i have the following problem: when i am writting the below command on the command prompt , its working. while read -n 1 ch; do echo "$ch" ; echo "$ch" ; done<file_name.out. but when i am executing it after saving it in a ksh file, its not working. Please helppppppppp .. thankss... (18 Replies)
Discussion started by: neelmani
18 Replies

5. UNIX for Dummies Questions & Answers

Reading character by character - BASH

Hello every one and thanks in advance for the time you will take to think about my problem. I would like to know if it's possible (in BASH) to read a text file character after character. Exactly this is what I would like to do : Txt file : ATGCAGTTCATTGCCAAA...... (~2.5 millions... (3 Replies)
Discussion started by: sluvah
3 Replies

6. Shell Programming and Scripting

Replace multiple occurances of same character with a single character.

Hi all, Greetings, I have the following scenario, The contents of main file are like : Unix|||||forum|||||||||||||||is||||||the||best so||||||be|||||on||||||||||||||||||||||||||||||||||||||||||||it And i need the output in the following form: Unix=forum=is=the=best so=be=on=it ... (3 Replies)
Discussion started by: dipanchandra
3 Replies

7. Shell Programming and Scripting

Reading a single character from each line of the file

Hi, I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file (8 Replies)
Discussion started by: arsii
8 Replies

8. Shell Programming and Scripting

Reading multiple lines as single

Hi, Is it possible to read every 2 lines as single record, I have a file like below, ~CZK ~TSCHECHISCHE KRONE ~TSCHECH. REPUBLIK Dec 1 2005 12:00AM~ 10.840000~ ~DKK ~DAENISCHE KRONE ~DAENEMARK Dec 2 2005 12:00AM~ ... (9 Replies)
Discussion started by: braindrain
9 Replies

9. Shell Programming and Scripting

Vi single character change

I know this is strictly a programming forum - but I consider vi a programming enabler and the question relates to regex you'd use with awk/sed anyway.... I have a file which is 50,000+ lines long and need to change many many instances of word_word_word to be word+word+word where... (6 Replies)
Discussion started by: Brett Taylor
6 Replies

10. Shell Programming and Scripting

reading a single line

hello, i need an algorithm which reads a line specified to it in shortest possible time. i am already using sed, i need a better way. please suggest me alternatives (2 Replies)
Discussion started by: rochitsharma
2 Replies
Login or Register to Ask a Question