LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 11-07-2002, 10:20 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
C flush() question ......


Hi there. I was just wondering why I have to flush stdin when doing fopen type operations in C. I am just learning how to write to disk using fopen and my book has me flush stdin between scanning for input for the file write and a filename for the file. Just basically wondering how come I don't have to ( or have not been told to) flush stdin in just plain old user input that doesn't write to disk such as >

printf ("Please enter your name: ");
scanf("%s", name );

Why wouldn't I have have to flush stdin here?

puts ("Enter your age");
scanf("%d", &age);
---------
as opposed to when I will be writing to a file using fopen() ...

puts("Enter an integer to save to disk");
scanf("%d", &number);

clear_kb(); /* little flushing function */

puts("Please enter a filename for the file");
scanf("%s", filename);


void clear_kb(void)
{
char junk[80];
gets(junk);
}

thanks
 
Old 11-07-2002, 12:40 PM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Not really answering your question here, but you don't need to write your own flushing function. Use fflush(FILE *stream)

Alex
 
Old 11-07-2002, 05:39 PM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Run this code:

Code:
#include<stdio.h>

int main()
{
  char fname[256];
  printf("Enter your first name: ");
  scanf("%s", fname);
  printf("%s", fname);
  printf("-newline\n");

  return 0;
}
You should be able to see from this that scanf leaves the newline character in the standard input buffer, instead of attaching it to the end of your name when you enter your name and press 'return'. That newline character, still inside the standard input buffer, will mess up subsequent calls to scanf, so it has to be cleared out of there. Not all I/O functions do this, for example try this code:
Code:
#include<stdio.h>

int main()
{
  char fname[256];
  printf("Enter your first name: ");
  fgets(fname,sizeof(fname),stdin);
  printf("%s", fname);
  printf("-newline?\n");

  return 0;
}
Notice the different output? This demonstrates that fgets does not leave the newline character in the standard input buffer.

This is how you might go about flushing the standard input buffer, note fflush(stdin) is undefined!
Code:
#include<stdio.h>

int main()
{
  char fname[256];
  char lname[256];
  printf("Enter your first name: ");
  scanf("%s", fname);
  while(getchar() != '\n') continue; //clear input buffer
  printf("Enter your last name: ");
  scanf("%s",lname);
  while(getchar() != '\n') continue;

  printf("Your name is %s %s\n.", fname, lname);
  return 0;
}
 
Old 11-15-2002, 09:27 AM   #4
flavor
LQ Newbie
 
Registered: Nov 2002
Location: Republic Of China
Posts: 9

Rep: Reputation: 0
flush() is not in standard C.
you can do it: gcc -o filename filename.c -lm
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
ARP flush abneru Suse/Novell 3 01-27-2012 12:57 PM
flush() before use... Elric of Grans Programming 1 07-12-2005 07:54 PM
How do I flush a C++ socket? pjz Programming 5 02-25-2005 10:55 AM
How can i flush iptables?? cranium2004 Linux - Networking 2 01-06-2005 02:08 PM
Full iptables rules flush question NetScripter Linux - General 7 09-17-2003 01:03 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration