Perdes

Untitled

Nov 30th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 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(FILE * fPointer);
  9. void WriteFile(char userWInput);
  10. void AppendFile(char AppendInput[999]);
  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 = '\0';
  21. char AppendInput = '\0';
  22. int userNInput;
  23. int i = '\0';
  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("Select choice\n> ");
  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(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(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.  
  98.  
  99. //DONE
  100. void AddRecord(int i, int userNInput, struct user record[100]) {
  101. for(i=0; i < userNInput; i++) {
  102. printf("Enter customerID\n> ");
  103. scanf(" %d", &record[i].customerID);
  104. printf("Enter timestamp\n> ");
  105. scanf(" %[^\n]s", record[i].timestamp);
  106. printf("Enter domain\n> ");
  107. scanf(" %s", record[i].domain);
  108.  
  109. printf("\n%s: User %d has visited %s\n\n", record[i].timestamp, record[i].customerID, record[i].domain);
  110. }
  111.  
  112. if (i == 1){
  113. printf("There is 1 record stored.");
  114. } else if (i > 1) {
  115. printf("There are %d records stored. ", i);
  116. } else {
  117. printf("There are no records stored.");
  118. }
  119. }
  120.  
  121.  
  122. // DONE
  123. void ReadFile(FILE * fPointer) {
  124.  
  125. char ch;
  126.  
  127. do
  128. {
  129. ch = fgetc(fPointer);
  130.  
  131. putchar(ch);
  132.  
  133. } while (ch != EOF);
  134. }
  135.  
  136.  
  137.  
  138. // IN PROGRESS
  139. void WriteFile(char WriteInput) {
  140. FILE *fPointer;
  141.  
  142. if( (fPointer = fopen( "WriteFile.ipb","w" )) != NULL )
  143.  
  144. {
  145. fputs( "TEST", fPointer );
  146. fputs( "\nTEST1", fPointer );
  147. // fputc( '\n', fPointer );
  148. fclose( fPointer );
  149. }
  150.  
  151. else
  152.  
  153. printf( "error message\n" );
  154. }
  155.  
  156.  
  157. // IN PROGRESS
  158. void AppendFile(char AppendInput[999]) {
  159.  
  160. FILE * fPointer;
  161. fPointer = fopen ("AppendFile.ipb", "a");
  162.  
  163. printf("Enter information\n> ");
  164. scanf(" %[^\n]s", AppendInput);
  165. fputs(AppendInput, fPointer);
  166. fputs("\n", fPointer);
  167. //fprintf(fPointer," %s", &AppendInput);
  168. fPointer = freopen("AppendFile.ipb", "r", fPointer);
  169. ReadFile(fPointer);
  170.  
  171. fclose(fPointer);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment