Posts

Snake, gun, water game in C(Version 1)

  #include <stdio.h> int snakegame( char you, char comp) {         if (comp == you)     {         return 0 ;     }     if (comp == 'w' && you == 's' )     {         return 1 ;     }     else if (comp == 's' && you == 'w' )     {         return - 1 ;     }     if (comp == 'w' && you == 'g' )     {         return - 1 ;     }     else if (comp == 'g' && you == 'w' )     {         return 1 ;     }     if (comp == 'g' && you == 's' )     {         return - 1 ;     }     else if (comp == 's' && you == 'g' )     {         return 1 ;     } } int main() { ...

updating number in a file through C program

  #include <stdio.h> int main(){ int num; FILE * ptr; ptr=fopen( "updatefile.txt" , "r" ); fscanf(ptr, "%d" ,&num); printf( "The value of num is %d" ,num); fclose(ptr); ptr=fopen( "updatefile.txt" , "w" ); fprintf(ptr, "%d" , 2 *num);   return 0 ; }

taking input from user and writing in a file in C

  #include <stdio.h> int main(){ FILE *ptr; float sal1,sal2; char name1[ 30 ],name2[ 30 ]; ptr=fopen( "employee.txt" , "w" ); printf( "Enter name of employe 1:\n" ); scanf( "%s" ,name1); printf( "Enter the salary of employee 1:\n" ); scanf( "%f" ,&sal1); printf( "Enter name of employee 2:\n" ); scanf( "%s" ,name2); printf( "Enter the salary of employee 2:\n" ); scanf( "%f" ,&sal2); printf( "%f %f %s %s" ,sal1,sal2,name1,name2); fprintf(ptr, "%s,%f\n" ,name1,sal1); fprintf(ptr, "%s,%f\n" ,name2,sal2); fclose(ptr);   return 0 ; }

reading a character from one file writing it twice in another file

  #include <stdio.h> int main(){ FILE *ptr1,*ptr2; ptr1=fopen( "file1.txt" , "r" ); ptr2=fopen( "file2.txt" , "w" ); char c; c=fgetc(ptr1); while (c!= EOF ) {     fputc(c,ptr2);     fputc(c,ptr2);     c=fgetc(ptr1);     } fclose(ptr1); fclose(ptr2);   return 0 ; }

writing a table in a file through c program

  #include <stdio.h> void table(FILE *ptr, int a) { for ( int i= 1 ;i<= 10 ;i++) { fprintf(ptr, "%d X %d = %d\n" ,a,i,(a*i)); } } int main(){ FILE * ptr; int a= 8 ; ptr=fopen( "tables.txt" , "w" ); fprintf(ptr, "The table of %d:\n" ,a); table(ptr,a); fclose(ptr);   return 0 ; }

reading 3 integers from a file in C

  #include <stdio.h> int main(){ FILE * ptr; ptr=fopen( "integer_file.txt" , "r" ); int num1,num2,num3; fscanf(ptr, "%d %d %d" ,&num1,&num2,&num3); printf( "The 3 integers are %d %d %d\n" ,num1,num2,num3); fclose(ptr);   return 0 ; }

reading whole file using file functions in C