Perdes

Code

Nov 30th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 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 = '\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(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.  
  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(char singleLine[150], FILE * fPointer) {
  124.  
  125.  
  126. while(!feof(fPointer)) { // Loops until file is printed.
  127. fgets(singleLine, 150, fPointer); // Read the file line by line
  128. puts(singleLine); // Print file line by line
  129. }
  130.  
  131. fclose(fPointer);
  132. }
  133.  
  134.  
  135.  
  136. // IN PROGRESS
  137. void WriteFile(char WriteInput) {
  138. FILE *fPointer;
  139.  
  140. if( (fPointer = fopen( "WriteFile.ipb","w" )) != NULL )
  141.  
  142. {
  143. fputs( "TEST", fPointer );
  144. fputs( "\nTEST1", fPointer );
  145. // fputc( '\n', fPointer );
  146. fclose( fPointer );
  147. }
  148.  
  149. else
  150.  
  151. printf( "error message\n" );
  152. }
  153.  
  154.  
  155. // IN PROGRESS
  156. void AppendFile(char AppendInput) {
  157.  
  158. FILE * fPointer;
  159. fPointer = fopen ("Example_Data.ipb", "a");
  160.  
  161. printf("Enter information\n> ");
  162. scanf(" %[^\n]s\n", &AppendInput);
  163. fprintf(fPointer," %s", &AppendInput);
  164.  
  165. fclose(fPointer);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment