M4ritimeSeeker

Competition

Nov 14th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. //MYSTERY PROGRAM
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdbool.h>
  6.  
  7. void FlushInput(void);
  8. bool function1(char*);
  9. bool function2(char*, char*);
  10.  
  11. int main(void){
  12.     FILE* fIn = NULL;
  13.     char textIn[300];
  14.     char fN[80];
  15.     char sv = 'y';
  16.     bool result = false;
  17.  
  18.     printf("Enter in some input:");
  19.  
  20.     if(fgets(textIn,300,stdin)){
  21.         printf("Your entered in %d characters.\n", (int) strlen(textIn));
  22.         printf("Enter in some more input: "); //Put main.c as input here on Replit.... if you dare :)
  23.         fscanf(stdin,"%s",fN);
  24.  
  25.             if(function1(fN) == true){
  26.                 printf("Continue? Press 'y' to continue:");
  27.                 FlushInput();
  28.                 scanf("%c", &sv);
  29.  
  30.                 if(sv == 'y')
  31.                     result = function2(textIn, fN);
  32.                 else
  33.                     printf("You chose not to continue.\n");
  34.             }
  35.             else{
  36.                 result = function2(textIn, fN);
  37.             }//end if-else
  38.     }
  39.     else {
  40.         printf("You did not enter in anything.\n");
  41.     }
  42.  
  43.     if(result)
  44.         printf("You successfully did the thing!");
  45.     else
  46.         printf("You did not successfully do the thing.");
  47.  
  48.     return 0;
  49. }//end main
  50.  
  51.  
  52. bool function1(char* fileN){
  53.     FILE* fIn = fopen(fileN,"r");
  54.  
  55.     if(fIn == NULL)
  56.         return false;
  57.     else{
  58.         fclose(fIn);
  59.         return true;
  60.     }
  61. }//end function
  62.  
  63.  
  64. void FlushInput(void){
  65.     while(getchar() != '\n');
  66.     return;
  67. }//end function
  68.  
  69.  
  70. bool function2(char* textIn, char* fName){
  71.     FILE* fPtr = fopen(fName,"w");
  72.  
  73.     if(fPtr == NULL){
  74.         printf("Error.\n");
  75.         return false;
  76.     }
  77.  
  78.     else{
  79.         fputs(textIn, fPtr);
  80.         fclose(fPtr);
  81.         return true;
  82.     }//end if-else
  83.  
  84. }//end function
Add Comment
Please, Sign In to add comment