Advertisement
froeling

local MicroSQL

Apr 24th, 2023 (edited)
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main(void)
  4. {
  5. char opr;
  6. int besk;
  7. char *name_file = malloc(sizeof(char) * 64);
  8. char *buffer_text = malloc(sizeof(char) * 512);
  9. FILE *file;
  10.  
  11. besk = 0;
  12.  
  13. printf("1 - write file, 2 - read file\n");
  14.  
  15. do
  16. {
  17. printf("What you need: ");
  18. scanf("%c", &opr);
  19.  
  20. switch (opr)
  21. {
  22. case '1':
  23. printf("Enter name for your file: ");
  24. scanf("%s", name_file);
  25. printf("Enter some text for your file: ");
  26. scanf("%s", buffer_text);
  27.  
  28. file = fopen(name_file, "w");
  29. fprintf(file, "%s", buffer_text);
  30. fclose(file);
  31.  
  32. free(name_file);
  33. free(buffer_text);
  34. break;
  35.  
  36. case '2':
  37. printf("Enter name for your file: ");
  38. scanf("%s", name_file);
  39.  
  40. file = fopen(name_file, "r");
  41. fgets(buffer_text, 512, file);
  42. fclose(file);
  43.  
  44. printf("Text from %s: %s", name_file, buffer_text);
  45. free(name_file);
  46. free(buffer_text);
  47. break;
  48.  
  49. case '0':
  50. besk = 1;
  51. break;
  52.  
  53. //default:
  54. //printf("Unknown command! Try again!\n");
  55. }
  56. }
  57. while (besk != 1);
  58.  
  59. name_file = NULL;
  60. buffer_text = NULL;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement