Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. //Name: Isaac Di Francesco
  6. //ID: 260868693
  7.  
  8. int main(int argc, char *argv[]) {
  9. //check if at least one argument was passed for CMD
  10. if (argc == 1) {
  11. puts("You did not provide any arguments. Please enter: ./database CMD OPT1 OPT2 OPT3 OPT4");
  12. return -1;
  13. }
  14.  
  15. //input variables
  16. char *CMD = argv[1];
  17. char *OPT1 = argv[2];
  18. char *OPT2 = argv[3];
  19. char *OPT3 = argv[4];
  20. char *OPT4 = argv[5];
  21. int ID, AGE;
  22. float GPA;
  23.  
  24. int charCount = 0, lineCount = 1, fieldCount = 0; //counters for loop
  25.  
  26. //check CMD input
  27. if (strcmp(CMD, "SHOW") == 0) {
  28. FILE *dataFile;
  29. dataFile = fopen("database.csv", "r");
  30.  
  31. //variables for each line
  32. char *line = NULL;
  33. size_t length = 0;
  34. char field1[100], field2[100], field3[100], field4[100];
  35.  
  36. //iterate through CSV file and get each line
  37. while (getline(&line, &length, dataFile) != -1) {
  38. //reset the field arrays
  39. memset(field1, 0, sizeof field1);
  40. memset(field2, 0, sizeof field2);
  41. memset(field3, 0, sizeof field3);
  42. memset(field4, 0, sizeof field4);
  43.  
  44. //update parameters
  45. fieldCount = 0;
  46. charCount = 0;
  47.  
  48. //get first field
  49. while (line[charCount] != ',') {
  50. //check for space
  51. if (line[charCount] != ' ') {
  52. field1[fieldCount] = line[charCount];
  53. fieldCount++;
  54. }
  55. charCount++;
  56. }
  57.  
  58. // update parameters
  59. charCount++;
  60. fieldCount = 0;
  61.  
  62. // get second field
  63. while (line[charCount] != ',') {
  64. //check for space
  65. if (line[charCount] != ' ') {
  66. field2[fieldCount] = line[charCount];
  67. fieldCount++;
  68. }
  69. charCount++;
  70. }
  71.  
  72. // update parameters
  73. charCount++;
  74. fieldCount = 0;
  75.  
  76. // get third field
  77. while (line[charCount] != ',') {
  78. //check for space
  79. if (line[charCount] != ' ') {
  80. field3[fieldCount] = line[charCount];
  81. fieldCount++;
  82. }
  83. charCount++;
  84. }
  85.  
  86. // update parameters
  87. charCount++;
  88. fieldCount = 0;
  89.  
  90. // get fourth field
  91. while (line[charCount] != '\n') {
  92. //check for space
  93. if (line[charCount] != ' ') {
  94. field4[fieldCount] = line[charCount];
  95. fieldCount++;
  96. }
  97. charCount++;
  98. }
  99.  
  100. //format fields to proper data type
  101. sscanf(field1, "%d", &ID);
  102. sscanf(field3, "%d", &AGE);
  103. sscanf(field4, "%f", &GPA);
  104.  
  105. //display record
  106. printf("Record %d: ID=%-3d NAME=%-5s AGE=%-3d GPA=%-3.1f \n", lineCount, ID, field2, AGE, GPA);
  107.  
  108. lineCount++;
  109. }
  110.  
  111. //free memory location for line & close file
  112. free(line);
  113. fclose(dataFile);
  114. } else if (strcmp(CMD, "DELETE") == 0) {
  115. //check if OPT1 has been passed
  116. if (argc <= 2) {
  117. printf("Name of record to delete is missing\n");
  118. return -1;
  119. }
  120.  
  121. // open database file
  122. FILE *dataFile;
  123. FILE *tempFile;
  124. dataFile = fopen("database.csv", "r");
  125. tempFile = fopen("database.tmp", "w");
  126.  
  127. //variables for each line
  128. char *line = NULL;
  129. size_t length = 0;
  130. char fieldCheck[100];
  131. int i = 0, j = 0;
  132. int notFound = 1;
  133.  
  134. //iterate through database file
  135. while (getline(&line, &length, dataFile) != -1) {
  136. i = 0;
  137. j = 0;
  138. while (line[i] != ',') {
  139. //check for space
  140. if (line[i] != ' ') {
  141. fieldCheck[j] = line[i];
  142. j++;
  143. }
  144. i++;
  145. }
  146.  
  147. //end string for further iterations of loop
  148. fieldCheck[j] = '\0';
  149.  
  150. // check if ID field from current line matches OPT1 and ID hasn't already been found
  151. if (strcmp(fieldCheck, OPT1) == 0 && notFound) {
  152. // exclude line from copy
  153. notFound = 0;
  154. } else {
  155. // copy line to temp file
  156. fputs(line, tempFile);
  157. }
  158. }
  159. if (notFound) {
  160. printf("Sorry, that user was not found. Nothing was deleted.\n");
  161. system("rm database.tmp");
  162. } else {
  163. system("mv database.tmp database.csv");
  164. }
  165. //free memory location of line & close files
  166. free(line);
  167. fclose(dataFile);
  168. fclose(tempFile);
  169. } else if (strcmp(CMD, "ADD") == 0) {
  170. // check if OPT variables have been passed
  171. if (argc < 6) {
  172. printf("Missing ID, Name, AGE, and GPA arguments\n");
  173. return -1;
  174. }
  175.  
  176. FILE *dataFile;
  177. dataFile = fopen("database.csv", "a");
  178.  
  179. //add line to datafile
  180. fputs(OPT1, dataFile);
  181. fputs(",", dataFile);
  182. fputs(OPT2, dataFile);
  183. fputs(",", dataFile);
  184. fputs(OPT3, dataFile);
  185. fputs(",", dataFile);
  186. fputs(OPT4, dataFile);
  187. fputs("\n", dataFile);
  188.  
  189. //close datafile
  190. fclose(dataFile);
  191. } else {
  192. printf("The command you requested is invalid. Please select from one of these: SHOW, DELETE, ADD\n");\
  193. return -1;
  194. }
  195.  
  196. return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement