The fflush() function writes any buffered data to the specified file stream. When you write data to a file (with a function such as fprintf()), it is actually placed in a memory buffer. The data is only actually written to the file when the buffer is full, the file stream is closed or when fflush() is called.
One of the standard C runtime-library functions.
The fflush() and flushall() functions in the C run-time library do not write file changes directly to disk. These functions flush the file buffers provided by the library; they do not flush the buffers the MS-DOS, OS/2, or Windows NT operating systems provide at the system level.In short clear the buffer memory.
/* Assuming that by "a shell program", you mean "a program that is run from a shell", and not "a shell script", then this would fit the bill. Note that it could be optimized in many ways, such as: - checking only for prime factors - only counting as high as the square root of the smaller number */ #include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ){ int val1, val2, n, gcd = 0; if( argc != 3 ){ fprintf( stderr, "Insufficent arguments. Syntax:\n\tgcf #a #b\n" ); fflush( stderr ); exit( 1 ); } val1 = atoi( argv[1] ); val2 = atoi( argv[2] ); if( val1 < 2 val2 < 2 ){ printf( "No common factors\n" ); exit( 0 ); } for( n = 2; n < val1 && n < val2; n++ ) { if( val1 % n 0 ){ printf( "No common factors\n" ); }else{ printf( "Greatest common factor is %i\n", gcd ); } return 0; }
/*implementing two stacks in 1 array*/ #include #include #include int size,top1=-1,top2,a[100]; void push(); void pop(); void peep(); void main() { int i,ch; char c='y'; clrscr(); printf("\Enter size:"); scanf("%d",&size); top2=size; while(c=='y') { clrscr(); printf("\n ###### MENU #####\n"); printf("\n1: push"); printf("\n2: pop"); printf("\n3: peep"); printf("\n4: exit") ; printf("\n Enter choice:"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:peep(); break; case 4:exit(0); default:printf("\n wrong choice"); } printf("\n Another operation(y/n):"); fflush(stdin); scanf("%c",&c); } exit(0); getch(); } void push() { int item,ch; if(top1+1==top2) { printf("stack is full "); printf("\n cant push "); } else { printf("\nEnter item to be inserted:"); scanf("%d",&item); printf("\n enter choice(1 for stack1 else stack2):"); scanf("%d",&ch); if(ch==1) { top1++; a[top1]=item; } else { top2--; a[top2]=item; } } } void pop() { int ch; printf("\n Enter from which stack u want to pop(1 for stack1/else stack2):"); scanf("%d",&ch); if(ch==1) { if(top1==-1) printf("\n cant delete from stack 1 its empty"); else printf("\n item deleted is:%d" ,a[top1--]); } else { if(top2==size) printf("\n cant delete from s2its empty"); else printf("\n item deleted is: %d" ,a[top2++]); } } void peep() { int i; printf("\n stck 1:" ); for(i=0;i=top2;i--) printf(" %d",a[i]); }
Commented code for a win32 console application: #include //Include stdio.h for console input/output funcs #include //Include conio.h for getch function (lol) void main( ) { int nWidth, nHeight; //Variables to hold width and height of rectangle while( 1 ) //Infinite loop - can only be exited using "break;" (among others) { printf( "Enter rectangle width: " ); //Print text... scanf( "%d", nWidth ); //Scan for user input fflush( stdin ); //Flush user input printf( "Enter rectangle height: " ); //Print text... scanf( "%d", nHeight ); //Scan for user input fflush( stdin ); //Flush user input printf( "Area of rectangle with width of %d and height of %d is %d\n", nWidth, nHeight, nWidth*nHeight ); //Print text... fflush( stdout ); //Flush output //Now program will jump back to start of loop } }
// Bus Reservation System # include <stdio.h> # include <conio.h> void install(); void reservation(); void show(); void allbus(); void removebus(); void position(int); struct bus { int bn; //Bus No. float bt; //Arrival Time char bd[20],from[10],to[10],seat[32][10]; //Driver Name,Journey From,Journey To }b; void install() { FILE *fp; fp=fopen("abc.dat", "ab"); clrscr(); if(fp==NULL) { printf("File Creation Failed!"); } else { printf("\n\t\tEnter Bus No.:= "); scanf("%d",&b.bn); printf("\n\t\tEnter Driver's Name:= "); fflush(stdin); scanf("%s",&b.bd); printf("\n\t\tEnter Arrival Time:= "); scanf("%f",&b.bt); printf("\n\t\tEnter Journey From:= "); fflush(stdin); scanf("%s",&b.from); printf("\n\t\tEnter Journey To:= "); fflush(stdin); scanf("%s",&b.to); fwrite(&b, sizeof(b), 1, fp); printf("\n\nBus Successfully Install"); } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void reservation() { int number,s; int flag=0; FILE *fp; fp=fopen("abc.dat","rb+"); clrscr(); printf("\n\nEnter Bus No:= "); scanf("%d",&number); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn==number) { printf("\nEnter Seat Number:= "); scanf("%d",&s); flag=1; if(s>32) { printf("\n\nThere are only 32 seats available in this bus"); break; } else { if(strcmp(&b.seat[s-1],NULL)==0) { printf("\nEnter Passenger's Name:= "); fflush(stdin); scanf("%s",&b.seat[s-1]); fseek(fp,ftell(fp)-sizeof(b),0); fwrite(&b,sizeof(b),1,fp); printf("\n\nReservation is Success"); break; } else { printf("\n\nThe seat no %d is already resevered",s); } } } } if(flag==0) { printf("\n\nBus Not Available"); } } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void show() { int number; int flag=0; FILE *fp; fp=fopen("abc.dat","rb"); clrscr(); printf("\n\nEnter Bus No:= "); scanf("%d",&number); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn==number) { printf("\nBus Number:= %d",b.bn); printf("\nBus Driver's Name:= %s",b.bd); printf("\nBus Arravial Time:= %.2f",b.bt); printf("\nBus Journey From:= %s",b.from); printf("\nBus Journey To:= %s",b.to); flag=1; position(b.bn); } } if(flag==0) { printf("\n\nBus Not Available"); } } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void position(int a) { int s=0,p=0; int i; printf("\n\n"); for(i=0;i<32;i++) { s++; if(strcmp(&b.seat[i],NULL)==0) { printf("%d. Empty\t",s); p++; } else { printf("%d. %s\t",s,b.seat[i]); } } printf("\n\nThere are %d seats empty in bus no. %d",p,a); } void allbus() { int no=0; FILE *fp; fp=fopen("abc.dat","rb"); clrscr(); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { printf("\n\n============================================================================="); no++; printf("\nBus Number:= %d",b.bn); printf("\nBus Driver's Name:= %s",b.bd); printf("\nBus Arravial Time:= %.2f",b.bt); printf("\nBus Journey From:= %s",b.from); printf("\nBus Journey To:= %s",b.to); printf("\n\n============================================================================="); printf("\n\nPress any key to Conti.."); getche(); } printf("\n\nTotal number of Bus(es) present:= %d",no); } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void removebus() { int number; int flag=0; char ch; FILE *fp,*ft; fp=fopen("abc.dat","rb"); ft=fopen("temp.dat","wb"); clrscr(); printf("\n\nEnter Bus No:= "); scanf("%d",&number); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn==number) { flag=1; printf("\nAre you sure want to Remove Bus %d ??(Y/N)",b.bn); fflush(stdin); scanf("%c", &ch); } } if(flag==0) { printf("\n\nBus Not Available"); } else { if(ch=='y'ch=='Y') { rewind(fp); while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn!=number) { fwrite(&b, sizeof(b), 1, ft); } } remove("abc.dat"); rename("temp.dat", "abc.dat"); printf("\n\nBus Remove Success"); } else { printf("\n\nBus Not Remove"); } } } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); fclose(ft); } void main() { int c; clrscr(); printf("\n============================================================================="); printf("\n\nDevloped By:= Chhatrala Chirag A. && Samani Jeki J."); printf("\n\nGuided By:= Jatin Sir"); printf("\n\n============================================================================="); printf("\n\n\t\t\tBUS RESERVATION SYSTEM"); printf("\n\n============================================================================="); while(1) { printf("\n\n\n"); printf("\t\t\t1.Install\n\t\t\t2.Reservation\n\t\t\t3.Show\n\t\t\t4.Bus(es) Available\n\t\t\t5.Remove Bus\n\t\t\t6.Exit"); printf("\n\n\t\t\tEnter Your Choice:=> "); scanf("%d",&c); switch(c) { case 1: install(); break; case 2: reservation(); break; case 3: show(); break; case 4: allbus(); break; case 5: removebus(); break; case 6: printf("\n\nThank You!!!!"); printf("\n\nPress any key to Conti.."); getche(); exit(0); break; default: printf("\n\t\t\tInvalid Choice\n\t\t\tTry Again...."); break; } } }
i supp fflush can only clear the buffer of 8 bits
The fflush() and flushall() functions in the C run-time library do not write file changes directly to disk. These functions flush the file buffers provided by the library; they do not flush the buffers the MS-DOS, OS/2, or Windows NT operating systems provide at the system level.In short clear the buffer memory.
#include<stdio.h> #include<conio.h> void main() { char *t,*str[10],s[10][50]; int i,j; str[0]=s[0]; clrscr(); printf("Input ten strings\n"); for(i=0;i<10;i++) { gets(s[i]); fflush(stdin); } for(i=0;i<10;i++) { for(j=i+1;j<10;j++) { if( ( strcmp( str[i],str[j]) ) > 0) { t=str[i]; str[i]=str[j]; str[j]=t; } } } for(i=0;i<10;i++) printf("%s",str[i]); getch(); } sunnydeep09
#include<stdio.h> void main() { int num,x,y,a[50][50]; clrscr(); fflush(stdin); printf("Enter the number of rows: "); scanf("%d",&num); printf("\n\t\tPascal's Triangle of Order %d\n\n",num); for(x=0;x<num;x++) { for(y=0;y<=x;y++) { if(x==y y==0) a[x][y]=1; else a[x][y]=a[x-1][y-1]+a[x-1][y]; printf("%4d",a[x][y]); } printf("\n\n"); } getch(); }
#include<stdio.h> #include<conio.h> void main() { char sender[50],receiver[50]; int i,winsize; clrscr(); printf("\n ENTER THE WINDOWS SIZE : "); scanf("%d",&winsize); printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW \n"); printf("\n ENTER THE DATA TO BE SENT: "); fflush(stdin); gets(sender); for(i=0;i<winsize;i++) receiver[i]=sender[i]; receiver[i]=NULL; printf("\n MESSAGE SEND BY THE SENDER:\n"); puts(sender); printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n"); printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n"); for(i=0;i<winsize;i++); printf("\n ACK:%d",i); printf("\n MESSAGE RECEIVED BY RECEIVER IS : "); puts(receiver); printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n"); getch(); }
/* Assuming that by "a shell program", you mean "a program that is run from a shell", and not "a shell script", then this would fit the bill. Note that it could be optimized in many ways, such as: - checking only for prime factors - only counting as high as the square root of the smaller number */ #include <stdio.h> #include <stdlib.h> int main( int argc, char *argv[] ){ int val1, val2, n, gcd = 0; if( argc != 3 ){ fprintf( stderr, "Insufficent arguments. Syntax:\n\tgcf #a #b\n" ); fflush( stderr ); exit( 1 ); } val1 = atoi( argv[1] ); val2 = atoi( argv[2] ); if( val1 < 2 val2 < 2 ){ printf( "No common factors\n" ); exit( 0 ); } for( n = 2; n < val1 && n < val2; n++ ) { if( val1 % n 0 ){ printf( "No common factors\n" ); }else{ printf( "Greatest common factor is %i\n", gcd ); } return 0; }
/*implementing two stacks in 1 array*/ #include #include #include int size,top1=-1,top2,a[100]; void push(); void pop(); void peep(); void main() { int i,ch; char c='y'; clrscr(); printf("\Enter size:"); scanf("%d",&size); top2=size; while(c=='y') { clrscr(); printf("\n ###### MENU #####\n"); printf("\n1: push"); printf("\n2: pop"); printf("\n3: peep"); printf("\n4: exit") ; printf("\n Enter choice:"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:peep(); break; case 4:exit(0); default:printf("\n wrong choice"); } printf("\n Another operation(y/n):"); fflush(stdin); scanf("%c",&c); } exit(0); getch(); } void push() { int item,ch; if(top1+1==top2) { printf("stack is full "); printf("\n cant push "); } else { printf("\nEnter item to be inserted:"); scanf("%d",&item); printf("\n enter choice(1 for stack1 else stack2):"); scanf("%d",&ch); if(ch==1) { top1++; a[top1]=item; } else { top2--; a[top2]=item; } } } void pop() { int ch; printf("\n Enter from which stack u want to pop(1 for stack1/else stack2):"); scanf("%d",&ch); if(ch==1) { if(top1==-1) printf("\n cant delete from stack 1 its empty"); else printf("\n item deleted is:%d" ,a[top1--]); } else { if(top2==size) printf("\n cant delete from s2its empty"); else printf("\n item deleted is: %d" ,a[top2++]); } } void peep() { int i; printf("\n stck 1:" ); for(i=0;i=top2;i--) printf(" %d",a[i]); }
Commented code for a win32 console application: #include //Include stdio.h for console input/output funcs #include //Include conio.h for getch function (lol) void main( ) { int nWidth, nHeight; //Variables to hold width and height of rectangle while( 1 ) //Infinite loop - can only be exited using "break;" (among others) { printf( "Enter rectangle width: " ); //Print text... scanf( "%d", nWidth ); //Scan for user input fflush( stdin ); //Flush user input printf( "Enter rectangle height: " ); //Print text... scanf( "%d", nHeight ); //Scan for user input fflush( stdin ); //Flush user input printf( "Area of rectangle with width of %d and height of %d is %d\n", nWidth, nHeight, nWidth*nHeight ); //Print text... fflush( stdout ); //Flush output //Now program will jump back to start of loop } }
// Bus Reservation System # include <stdio.h> # include <conio.h> void install(); void reservation(); void show(); void allbus(); void removebus(); void position(int); struct bus { int bn; //Bus No. float bt; //Arrival Time char bd[20],from[10],to[10],seat[32][10]; //Driver Name,Journey From,Journey To }b; void install() { FILE *fp; fp=fopen("abc.dat", "ab"); clrscr(); if(fp==NULL) { printf("File Creation Failed!"); } else { printf("\n\t\tEnter Bus No.:= "); scanf("%d",&b.bn); printf("\n\t\tEnter Driver's Name:= "); fflush(stdin); scanf("%s",&b.bd); printf("\n\t\tEnter Arrival Time:= "); scanf("%f",&b.bt); printf("\n\t\tEnter Journey From:= "); fflush(stdin); scanf("%s",&b.from); printf("\n\t\tEnter Journey To:= "); fflush(stdin); scanf("%s",&b.to); fwrite(&b, sizeof(b), 1, fp); printf("\n\nBus Successfully Install"); } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void reservation() { int number,s; int flag=0; FILE *fp; fp=fopen("abc.dat","rb+"); clrscr(); printf("\n\nEnter Bus No:= "); scanf("%d",&number); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn==number) { printf("\nEnter Seat Number:= "); scanf("%d",&s); flag=1; if(s>32) { printf("\n\nThere are only 32 seats available in this bus"); break; } else { if(strcmp(&b.seat[s-1],NULL)==0) { printf("\nEnter Passenger's Name:= "); fflush(stdin); scanf("%s",&b.seat[s-1]); fseek(fp,ftell(fp)-sizeof(b),0); fwrite(&b,sizeof(b),1,fp); printf("\n\nReservation is Success"); break; } else { printf("\n\nThe seat no %d is already resevered",s); } } } } if(flag==0) { printf("\n\nBus Not Available"); } } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void show() { int number; int flag=0; FILE *fp; fp=fopen("abc.dat","rb"); clrscr(); printf("\n\nEnter Bus No:= "); scanf("%d",&number); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn==number) { printf("\nBus Number:= %d",b.bn); printf("\nBus Driver's Name:= %s",b.bd); printf("\nBus Arravial Time:= %.2f",b.bt); printf("\nBus Journey From:= %s",b.from); printf("\nBus Journey To:= %s",b.to); flag=1; position(b.bn); } } if(flag==0) { printf("\n\nBus Not Available"); } } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void position(int a) { int s=0,p=0; int i; printf("\n\n"); for(i=0;i<32;i++) { s++; if(strcmp(&b.seat[i],NULL)==0) { printf("%d. Empty\t",s); p++; } else { printf("%d. %s\t",s,b.seat[i]); } } printf("\n\nThere are %d seats empty in bus no. %d",p,a); } void allbus() { int no=0; FILE *fp; fp=fopen("abc.dat","rb"); clrscr(); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { printf("\n\n============================================================================="); no++; printf("\nBus Number:= %d",b.bn); printf("\nBus Driver's Name:= %s",b.bd); printf("\nBus Arravial Time:= %.2f",b.bt); printf("\nBus Journey From:= %s",b.from); printf("\nBus Journey To:= %s",b.to); printf("\n\n============================================================================="); printf("\n\nPress any key to Conti.."); getche(); } printf("\n\nTotal number of Bus(es) present:= %d",no); } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); } void removebus() { int number; int flag=0; char ch; FILE *fp,*ft; fp=fopen("abc.dat","rb"); ft=fopen("temp.dat","wb"); clrscr(); printf("\n\nEnter Bus No:= "); scanf("%d",&number); if(fp==NULL) { printf("\n\tFile doesn't exist!!!\TRY AGAIN"); } else { while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn==number) { flag=1; printf("\nAre you sure want to Remove Bus %d ??(Y/N)",b.bn); fflush(stdin); scanf("%c", &ch); } } if(flag==0) { printf("\n\nBus Not Available"); } else { if(ch=='y'ch=='Y') { rewind(fp); while((fread(&b, sizeof(b), 1, fp))==1) { if(b.bn!=number) { fwrite(&b, sizeof(b), 1, ft); } } remove("abc.dat"); rename("temp.dat", "abc.dat"); printf("\n\nBus Remove Success"); } else { printf("\n\nBus Not Remove"); } } } printf("\n\nPress any key to Conti.."); getche(); fclose(fp); fclose(ft); } void main() { int c; clrscr(); printf("\n============================================================================="); printf("\n\nDevloped By:= Chhatrala Chirag A. && Samani Jeki J."); printf("\n\nGuided By:= Jatin Sir"); printf("\n\n============================================================================="); printf("\n\n\t\t\tBUS RESERVATION SYSTEM"); printf("\n\n============================================================================="); while(1) { printf("\n\n\n"); printf("\t\t\t1.Install\n\t\t\t2.Reservation\n\t\t\t3.Show\n\t\t\t4.Bus(es) Available\n\t\t\t5.Remove Bus\n\t\t\t6.Exit"); printf("\n\n\t\t\tEnter Your Choice:=> "); scanf("%d",&c); switch(c) { case 1: install(); break; case 2: reservation(); break; case 3: show(); break; case 4: allbus(); break; case 5: removebus(); break; case 6: printf("\n\nThank You!!!!"); printf("\n\nPress any key to Conti.."); getche(); exit(0); break; default: printf("\n\t\t\tInvalid Choice\n\t\t\tTry Again...."); break; } } }
struct node{ int data; struct node *left, *right; }; typedef struct node node; non recursive method : main() { node * root = NULL , *new = NULL *temp1 =NULL , * temp2 = NULL; int num =1; printf(" Enter the elements of the tree( enter 0 to exit)\n"); while(1) { scanf("%d", &num); if(num==0) break; new = malloc(sizeof(node)); new->left = new->right = NULL; new->data = num; if( root NULL) root->left = new; else insert( new,root->left); } }
Here is the required code.... to count the no. of nodes in a singly linked list.. #include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h> struct linear_list { int info; struct linear_list *next; }*start,*newnode,*ptr; void main() { int item,num; int i; char ch; clrscr(); newnode=(struct linear_list*)malloc(sizeof(struct linear_list)); start=newnode; do { printf("\nEnter data: "); scanf("%d",&item); newnode->info=item; printf("\nDo you want to create another node:(y/n)"); fflush(stdin); scanf("%c",&ch); if(tolower(ch)=='y') { newnode->next=(struct linear_list*)malloc(sizeof(struct linear_list)); newnode=newnode->next; } else { newnode->next=NULL; } }while(tolower(ch)!='n'); printf("\n Linked List is:\n"); ptr=start; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } num=0; ptr=start; while(ptr!=NULL) { ptr=ptr->next; num++; } printf("\nThe number of nodes in the linked list are: %d",num); getch(); }
#include #include #include #include #define FIELD_SIZE 25#define length(array) ( sizeof (array) / sizeof (array)[i] )struct account {char *id;char *password;};static struct account accounts[] = {{ "11user1", "Pwd1" },{ "12user2", "Pwd2" },{ "13user3", "Pwd3" }};int is_authorized ( const char *uid, const char *pwd ){int i;for ( i = 0; i < length ( accounts ); i++ ) {if ( stricmp ( uid, accounts[i].id ) == 0 &&strcmp ( pwd, accounts[i].password ) ==0 ){return 1;}}return 0;}void get_password ( char *pwd, int size ){int i = 0;int ch;while ( i < size - 1 && ( ch = getch() ) != '\r' ) {if ( ch == '\b' ) {/* Don't run off the front end of the array */if ( i != 0 ) {printf ( "\b%c\b", ' ' );--i;}}else {putchar ( '*' );pwd[i++] = (char)ch;}}pwd[i] = '\0';}int main ( void ){char uid[FIELD_SIZE];char pwd[FIELD_SIZE];printf ( "User ID: " );fflush ( stdout );if ( fgets ( uid, sizeof uid, stdin ) != NULL ) {char *newline = strchr ( uid, '\n' );/* Trim the newline if it's present */if ( newline != NULL )*newline = '\0';printf ( "Password: " );fflush ( stdout );get_password ( pwd, sizeof pwd );if ( is_authorized ( uid, pwd ) )printf("\n\n\t\tUSER AUTHENTICATED\n");elseprintf("\n\n\t\tINVALID USER\n");}return 0;}