LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2002, 09:58 AM   #1
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Rep: Reputation: 0
Question simple c programming problem


i have a very silly problem with c programming in linux.i have red hat 6.2 linux version.and whenever i am writing a code for accepting a single character its not accepting it, rather it is skiping the value.i also used fflush(stdin),but problem not solved.the same code runs perfectly in TC(dos based).what can be the problem can any one tell me?



i used scanf and getchar both but these both never worked

Last edited by abi_sh; 07-01-2002 at 09:14 AM.
 
Old 06-25-2002, 10:23 AM   #2
Frustin
Member
 
Registered: May 2002
Location: Essex, UK
Distribution: Debian, Redhat, AIX 5L
Posts: 512

Rep: Reputation: 30
Re: simple c programming problem

Quote:
Originally posted by abi_sh
i have a very silly problem with c programming in linux.i have red hat 6.2 linux version.and whenever i am writing a code for accepting a single character its not accepting it, rather it is skiping the value.i also used fflush(stdin),but problem not solved.the same code runs perfectly in TC(dos based).what can be the problem can any one tell me?
how about using getchar()?

Last edited by Frustin; 06-25-2002 at 10:25 AM.
 
Old 06-25-2002, 11:05 AM   #3
sakeeb
Member
 
Registered: Apr 2002
Posts: 270

Rep: Reputation: 31
which is the function you used to get the character?
 
Old 06-28-2002, 08:09 AM   #4
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Original Poster
Rep: Reputation: 0
yeah i have treid getchar() also it doesnt' work even then.
 
Old 07-19-2002, 09:05 AM   #5
edreddy
Member
 
Registered: Jul 2002
Location: INDIA, Bangalore.
Distribution: Red Hat Enterprise Linux 3.0 Advanced Server
Posts: 34

Rep: Reputation: 15
hi abi_sh,
yes it is the problem in linux. fflush(stdin) doesn't work in linux.
the reason why it is skipping that getting of char is there might be something garbage in stdin buffer. the garbage is due to previous scanf()'s or gets()'s before getchar(). so always use fgets() instead of scanf()'s and getchar()'s.
now try to replace to scanfs()'s and gets()'s with fgets();
if you are in need of formatted input , now try to format the input using atoi() or strtod() or something else.
now use getchar(), i hope there won't be problem.
 
Old 07-19-2002, 01:47 PM   #6
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
When removing garbage you should also remember that in DOS/Windows end of line is 2 characters, when in Unix world it's only 1 character long.
 
Old 07-24-2002, 10:15 AM   #7
abi_sh
LQ Newbie
 
Registered: Jun 2002
Posts: 9

Original Poster
Rep: Reputation: 0
hi edreddy
i tried the fgets() function also but it doesn't work it is giving me a error saying that missing parameters or something,well as far as i know fgets is used with FILE pointer right ,it takes file pointer.
if u don't mind can u write a small code with this fgets() and show how to use it?
 
Old 07-24-2002, 10:53 AM   #8
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
Try something like
#include <stdio.h>
main(){
char a;
a=fgetc(stdin);
printf("%c\n",a);
}
--Sarin
 
Old 07-24-2002, 08:40 PM   #9
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Here is a code example of how to use getchar(). It is pretty much the same as the fgetc example above.
Code:
#include <stdio.h>
#include <unistd.h>

main()
{
   char c = (char) NULL;

   while ( 1 )
   {
      c = getchar();
      if ( c != -1 )
         printf("Hey there\n");
      usleep(100);

   }  // end while
}
fgets example:
Code:
#include <stdio.h>
#include <fnmatch.h>
#include <unistd.h>

#define BUFF_SIZE 256
main()
{
   char szInputBuffer[BUFF_SIZE + 1];
   int iMatch = 0;

   fgets( szInputBuffer, BUFF_SIZE, stdin );

   if (( strncasecmp( "y", szInputBuffer, 1 ) == 0) ||
       ( strncasecmp( "yes", szInputBuffer, 3 ) == 0))
   {
      iMatch = 1;
   }
   printf("String is [%s]; iMatch is [%d]\n", szInputBuffer, iMatch );
}
The problem when you have to press enter to get single character input is actually a terminal problem and not a fgetc or getchar error. Most terminals by default are in canonical mode, meaning that you have to press enter to send the input line. You can turn off canonical mode into ways.

Using stty:
Code:
stty -icanon
Run the first example above, before and after running stty.

Change conon setting with a program:
Best read in the UNIX FAQ:
http://www.erlenstar.demon.co.uk/unix/faq_4.html#SEC49

Last edited by crabboy; 07-24-2002 at 08:41 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple shall programming question itz2000 Programming 1 11-05-2005 04:12 PM
help with programming a simple ls command in C babyboss Programming 4 10-19-2005 05:01 PM
Simple C programming Spooky Programming 5 10-22-2004 03:16 AM
A Simple Programming IDE for X. xarius Programming 8 02-25-2004 12:10 PM
Simple C Programming Question.. Bolt Programming 6 06-03-2003 12:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration