Scanf problem under LINUX...

Thread Tools Search this Thread
Top Forums Programming Scanf problem under LINUX...
# 1  
Old 04-16-2002
Question Scanf problem under LINUX...

I have a problem reading characters from keyboard with the scanf function.
Here there is a little piece of code:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

/* The last 3 libraries are included because in the real program I use some operations with sockets (socket, connect, getsockname, send, recv...) but I'm sure that the problem isn't here... */

int main()
char Ch = '0', Ch_Seq[64], inbuf[2];


printf("Insert a character ('q' = Exit): ");
scanf("%c", &Ch);

while (Ch != 'q')
printf("Insert a string: ");
scanf("%s", Ch_Seq);
/* There isn't length control, I know. Suppose to enter a valid string. */

/* Asks for a new character */
printf("\nInsert a character ('q' = Exit): ");
scanf("%c", &Ch);

printf("End of program.\n\n");

This program (except for socket operations, obviously) works perfectly under MS-DOS, but when I try to run it under Linux (Mandrake 8.0) the second scanf functions in the while loop fails, because it reads the enter character of the previous scanf (which works correctly and stores the string in Ch_Seq).
For this reason the program can't never read the 'q' character, and enters in an infinite loop.

I have noticed that the only scanf that works correctly is the first, the one before the program enters in the while loop. Infact, if I run the program and I immediately enter a 'q' character, the program ends, writing "End of program.".

I have tried to use
scanf("%c\n", &Ch);
instead of
scanf("%c", &Ch);
but it doesn't work.

I have also tried with
Ch = getchar();
but the result is the same as described before.

I have temporary solved the problem replacing all the occurencies of
scanf("%c", &Ch);
read(0, inbuf, 2);
Ch = inbuf[0];

With this solution I have noticed a strange thing... If I write
read(stdin, inbuf, 2);
instead of
read(0, inbuf, 2);
it doesn't work! Smilie


However, I want to use anyway the scanf function and understand why under Linux it doesn't work.
Is there someone that could explain me in detail the behaviour of scanf function (and fflush) in Linux and how can I solve the problem?

I need to use scanf also because I want to port under Linux some programs written for MS-DOS that makes large use of this function.

I hope you can help me!

Thanks in advance, and excuse for my poor english! Smilie
# 2  
Old 04-16-2002
Change the "scanf %c" to "scanf %s" and see what happens....
# 3  
Old 04-16-2002

Well, if I use scanf("%s", &Ch) the program works correctly, but the 'enter' character and the terminating character '\0' where are stored?
Isn't this an error? Don't these two characters overwrite the two bytes of memory next to the "Ch" variable?
If the method you shown me is ok, then when do we need to use "%c" or other functions that reads only a character (like getchar) ?
I still don't understand... Smilie

Another question: I want to read a string, scanning also spaces and tabs and check the length (and if necessary prompt the user to re-enter the string). Is the use of fgets followed by sscanf the best method I can use? Or are there any better methods?

Thanks a lot for your help! Smilie
# 4  
Old 04-16-2002
Yes, ideally you should increase the length of your input buffer since scanf will not consume the trailing newline. You may wish to read the "stdio" section of the C FAQ, in particular, question 12.20.
# 5  
Old 04-19-2002

Thanks a lot for your help and for the "C FAQ" link, it's very useful!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Scanf() string pointer problem

I have a problem with scanf() for string pointer as member of a struct. #include <stdio.h> #include <stdlib.h> struct Student { int studentNumber; int phoneNumber; char *studentName; //line 7 // char studentName; //line 8 }; int... (10 Replies)
Discussion started by: yifangt
10 Replies

2. Shell Programming and Scripting

Passing argument 1 of 'scanf' makes po

$ cc Array.c Array.c: In function ‘main’: Array.c:23: warning: passing argument 1 of ‘scanf’ makes po Array.c:25: error: expected expression before ‘return’ Array.c:29: error: expected expression before ‘return’ Array.c: At top level: Array.c:44: error: expected ‘)’ before ‘&’ token... (8 Replies)
Discussion started by: sgradywhite
8 Replies

3. Programming

What is the difference between printf and putchar() or scanf and getchar() ?

Im a newbie to programming language, i found tat there r these function called printf and putchar() as well as scanf and getchar(), im curious abt why do dey hav these 2 different function although dey r doing the same instruction? :confused: (13 Replies)
Discussion started by: kris26
13 Replies

4. Programming

Better than scanf

I don't know how to do this: printf("creazione nuovo messaggio\n"); printf("insert dest\n"); scanf("%s",dest); printf("insert object\n"); scanf("%s",ogg); printf("inserire text\n"); scanf("%s",test); ... (7 Replies)
Discussion started by: italian_boy
7 Replies

5. Programming

simple scanf issue ?

Hello everyone, I hope someone is awake to help me on this.. hey How can I do something like this: The user is asked is asked to enter an int value, but I want to provide a default value on stdout, which they can back space and change it to whatever they want.. for e.g: Enter the... (4 Replies)
Discussion started by: the_learner
4 Replies

6. Programming

problem with scanf

hi all! i've written a simple c program: #include<stdio.h> #include<stdlib.h> int main() { int a; char b; char c; ... (4 Replies)
Discussion started by: mridula
4 Replies

7. Programming

scanf doesn´t reads spaces ???

hi all i have a program in C (Unix Solaris 5.7) and i want to read a string from keyboard, but the "scanf" doesn´t reads spaces. example: .... char name; .... printf("Enter your name: "); scanf("%s",&name); printf ("Your name is: %s", name); and if i write Kevin Costner ... (4 Replies)
Discussion started by: DebianJ
4 Replies

8. Programming

scanf with strings... please help

hi i am a beginner to C i have encountered a problem with my assignment, and i have researched it on the internet, but unfortunately i didn't find anything related to that. i am writing a simple program that takes user's input by prompt command, and parse the whole line into an array of... (1 Reply)
Discussion started by: inkfish
1 Replies

9. UNIX for Dummies Questions & Answers

Linux Redhat 8.0 Problem

can someone tell me what commands in Linux RedHat 8.0 will give me a graph of whats going on on my system. When Is ay graphs I mean graphs like does produced by perfview of solaris and glance of HP-UX. (3 Replies)
Discussion started by: TRUEST
3 Replies

10. UNIX for Dummies Questions & Answers

linux boot problem

i have instaled a boot loader and win98 on drive c: and linux on drive d: and when i go to boot with the loader into linux nottin there some way i should install lilo to let it boot of a loader on drive c: ? (5 Replies)
Discussion started by: perleo
5 Replies
Login or Register to Ask a Question