Perdes

Untitled

Nov 29th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "structs.h"
  4.  
  5. // void PrintRecord(struct user ben); TEST
  6. int CaseCheck(char a, char b);
  7. void AddRecord(int i, int userNInput, struct user record[100]);
  8. void ReadFile(char singleLine[150], FILE * fPointer);
  9. void WriteFile(char userWInput);
  10. void AppendFile(char AppendInput);
  11.  
  12. int main()
  13. {
  14. /*struct user ben;
  15. struct user emily; */ // TEST
  16. struct user record[100];
  17.  
  18. char userInput;
  19. char singleLine[150];
  20. char userWInput;
  21. char AppendInput;
  22. int userNInput;
  23. int i;
  24. FILE * fPointer;
  25.  
  26.  
  27. while(1) {
  28. printf("\nWelcome to the main menu");
  29. printf("\n\nA) Add new record \nB) Read file\nC) Write File\nD) Append file\nE) Read written file\nQ) Quit\n\n");
  30. printf("Enter choice: ");
  31. scanf(" %c", &userInput);
  32.  
  33. if(CaseCheck(userInput, 'a')) {
  34.  
  35. printf("\nAdd new record\n");
  36. printf("How many records do you want to add: ");
  37. scanf("%d", &userNInput);
  38. AddRecord(i, userNInput, record);
  39.  
  40. } else if (CaseCheck(userInput, 'b')) {
  41.  
  42. printf("\nRead file\n\n");
  43. fPointer = fopen ("Example_Data.ipb", "r"); //Opens file. 'r' means to read the file.
  44. ReadFile(singleLine, fPointer);
  45.  
  46. } else if (CaseCheck(userInput, 'c')) {
  47.  
  48. printf("\nWrite File\n");
  49. WriteFile(userWInput);
  50.  
  51. } else if (CaseCheck(userInput, 'd')) {
  52.  
  53. printf("\nAppend file\n");
  54. AppendFile(AppendInput);
  55.  
  56. } else if (CaseCheck(userInput, 'e')) {
  57.  
  58. printf("\nRead file(s)\n");
  59. fPointer = fopen ("WriteFile.ipb", "r"); //Opens file. 'r' means to read the file.
  60. ReadFile(singleLine, fPointer);
  61.  
  62. } else if (CaseCheck(userInput, 'q')) {
  63. exit(0);
  64. }
  65.  
  66. }
  67. /* printf("\nEnter customerID: ");
  68. scanf(" %d", &ben.customerID);
  69.  
  70. printf("\nEnter time in 2016-12-11 12:34:56 format: ");
  71. scanf(" %[^\n]s", ben.timestamp); // '[^\n]' allows the user to type information including whitespaces.
  72.  
  73. printf("\nEnter domain: ");
  74. scanf("%s", ben.domain);
  75.  
  76. PrintRecord(ben); */ // TEST
  77.  
  78. return 0;
  79. }
  80.  
  81. /* void PrintRecord(struct user ben)
  82. {
  83. printf("\ncustomerID: %d\n", ben.customerID);
  84. printf("TimeStamp: %s\n", ben.timestamp);
  85. printf("Domain: %s\n", ben.domain);
  86. printf("%s: User %d visited %s\n", ben.timestamp, ben.customerID, ben.domain);
  87. }*/ // TEST
  88.  
  89. int CaseCheck(char a, char b) {
  90. if (a==b || a+32 == b || a == b+32){
  91. return 1;
  92. } else {
  93. return 0;
  94. }
  95. }
  96.  
  97. void AddRecord(int i, int userNInput, struct user record[100]) {
  98. for(i=0; i < userNInput; i++) {
  99. printf("Enter customerID\n> ");
  100. scanf(" %d", &record[i].customerID);
  101. printf("Enter timestamp\n> ");
  102. scanf(" %[^\n]s", record[i].timestamp);
  103. printf("Enter domain\n> ");
  104. scanf(" %s", record[i].domain);
  105.  
  106. printf("\n%s: User %d has visited %s\n\n", record[i].timestamp, record[i].customerID, record[i].domain);
  107. }
  108.  
  109. if (i == 1){
  110. printf("There is 1 record stored.");
  111. } else if (i > 1) {
  112. printf("There are %d records stored. ", i);
  113. } else {
  114. printf("There are no records stored.");
  115. }
  116. }
  117.  
  118.  
  119. // DONE
  120. void ReadFile(char singleLine[150], FILE * fPointer) {
  121.  
  122.  
  123. while(!feof(fPointer)) { // Loops until file is printed.
  124. fgets(singleLine, 150, fPointer); // Read the file line by line
  125. puts(singleLine); // Print file line by line
  126. }
  127.  
  128. fclose(fPointer);
  129. }
  130.  
  131.  
  132.  
  133. // IN PROGRESS
  134. void WriteFile(char WriteInput) {
  135. FILE * fPointer;
  136. fPointer = fopen("WriteFile.ipb", "w");
  137. fprintf(fPointer,"This is a written file.\n");
  138.  
  139. fclose(fPointer);
  140. }
  141.  
  142.  
  143. // IN PROGRESS
  144. void AppendFile(char AppendInput) {
  145.  
  146. FILE * fPointer;
  147. fPointer = fopen ("Example_Data.ipb", "a");
  148.  
  149. printf("Enter information\n> ");
  150. scanf(" %s", &AppendInput);
  151. fprintf(fPointer," %s", &AppendInput);
  152.  
  153. fclose(fPointer);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment