Sponsored Content
Full Discussion: Scanf problem under LINUX...
Top Forums Programming Scanf problem under LINUX... Post 19725 by robotronic on Tuesday 16th of April 2002 07:42:39 AM
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];

system("clear");

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

while (Ch != 'q')
{
printf("Insert a string: ");
fflush(0);
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): ");
fflush(0);
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
fflush(0);
scanf("%c", &Ch);
with
fflush(stdout);
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

Why?



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
 

10 More Discussions You Might Find Interesting

1. 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 happens.is 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
mvscanw(3XCURSES)					  X/Open Curses Library Functions					 mvscanw(3XCURSES)

NAME
mvscanw, mvwscanw, scanw, wscanw - convert formatted input from a window SYNOPSIS
cc [ flag... ] file... -I /usr/xpg4/include -L /usr/xpg4/lib -R /usr/xpg4/lib -lcurses [ library... ] c89 [ flag... ] file... -lcurses [ library... ] #include <curses.h> int mvscanw(int y, int x, char *fmt, ...); int mvwscanw(WINDOW *win, int y, int x, char *fmt, ...); int scanw(char *fmt, ...); int wscanw(WINDOW *win, char *fmt, ...); PARAMETERS
y Is the y (row) coordinate of the position of the character to be read. x Is the x (column) coordinate of the position of the character to be read. fmt Is a scanf() format string. win Is a pointer to the window in which the character is to be read. DESCRIPTION
These functions are similar to scanf(3C). Their effect is as though mvwgetstr(3XCURSES) were called to get a multi-byte character string from the current or specified window at the current or specified cursor position, and then sscanf() were used to interpret and convert that string. RETURN VALUES
Upon successful completion, these functions return OK. Otherwise, they return ERR. ERRORS
No errors are defined. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
getnstr(3XCURSES), libcurses(3XCURSES), printw(3XCURSES), scanf(3C), wcstombs(3C), attributes(5), standards(5) SunOS 5.10 5 Jun 2002 mvscanw(3XCURSES)
All times are GMT -4. The time now is 12:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy