fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5.  
  6. int MenuLoop = 0;
  7. int MaxPackets = 4;
  8. int currentPackets= 0;
  9. int menu;
  10.  
  11.  
  12.  
  13. /*********************************************************
  14. * Node to represent a Cat which includes a link reference*
  15. * a link list of nodes with a pointer to a Cat Struct *
  16. * would be better but this is for illustartion only! *
  17. **********************************************************/
  18. struct Packet {
  19. int Source;
  20. int Destination;
  21. int Type;
  22. int Port;
  23. char *Data;
  24. struct Packet *next; // Link to next Cat
  25. };
  26.  
  27. typedef struct Packet node; // Removes the need to constantly refer to struct
  28.  
  29.  
  30. /*********************************************************
  31. * Stubs to fully declared functions below *
  32. **********************************************************/
  33. void outputPackets(node **head);
  34. void push(node **head, node **aPacket);
  35. node* pop(node **head);
  36. void AddPacket();
  37. void AddPacket();
  38. void SavePacket();
  39. void ShowCurrent();
  40. void ExitProgramme();
  41.  
  42.  
  43.  
  44. main() {
  45.  
  46. do{
  47.  
  48. Menu();
  49.  
  50. } while(menu<4);
  51.  
  52.  
  53. }
  54.  
  55.  
  56. void AddPacket(){
  57.  
  58. int option;
  59.  
  60.  
  61.  
  62.  
  63.  
  64. /*********************************************************
  65. * pointers for the link list and the temporary P to *
  66. * insert into the list *
  67. **********************************************************/
  68. node *pPacket, *pHead = NULL;
  69.  
  70. /*********************************************************
  71. * Create a cat and also check the HEAP had room for it *
  72. **********************************************************/
  73. pPacket = (node *)malloc(sizeof(node));
  74. if (pPacket == NULL)
  75. {
  76. printf("Error: Out of Memory\n");
  77. exit(1);
  78. }
  79.  
  80. currentPackets++;
  81. printf("Enter Source Number between 1-1024:\n");
  82.  
  83. scanf("%i", &pPacket->Source);
  84. printf("Enter Destination Number between 1-1024:\n");
  85. scanf("%i", &pPacket->Destination);
  86. printf("Enter Type Number between 0-10:\n");
  87. scanf("%i", &pPacket->Type);
  88. printf("Enter Port Number between 1-1024:\n");
  89. scanf("%i", &pPacket->Port);
  90. printf("Enter Data Numberbetween 1-50:\n");
  91. scanf("%s", &pPacket->Data);
  92. printf("Do you want to Enter another Packet?");
  93. pPacket->next = NULL;
  94.  
  95. /*********************************************************
  96. * Push the Cat onto the selected Link List, the function *
  97. * is written so the program will support multiple link *
  98. * list if additional 'pHead' pointers are created. *
  99. * Who says you cannot herd cats! *
  100. **********************************************************
  101. * NOTE: The push parameters are using references to the *
  102. * pointers to get round the pass by value problem caused *
  103. * by the way C handles parameters that need to be *
  104. * modified *
  105. **********************************************************/
  106.  
  107. push(&pHead, &pPacket);
  108.  
  109. pPacket = (node *)malloc(sizeof(node));
  110. if (pPacket == NULL)
  111. {
  112. printf("Error: Out of Memory\n");
  113. exit(1);
  114. }
  115.  
  116. outputPackets(&pHead);
  117.  
  118. /*********************************************************
  119. * Display the Link List 'pHead' is passed as a reference *
  120. **********************************************************/
  121.  
  122.  
  123. return 0;
  124.  
  125.  
  126. do{
  127. if(currentPackets == MaxPackets);
  128. {
  129. printf("Packet limit reached please save\n");
  130.  
  131. }
  132.  
  133.  
  134. }while(currentPackets<MaxPackets);
  135.  
  136. return 0;
  137. }
  138.  
  139.  
  140.  
  141.  
  142. void outputPackets(node **head)
  143. {
  144.  
  145.  
  146. /*********************************************************
  147. * Copy Node pointer so as not to overwrite the pHead *
  148. * pointer *
  149. **********************************************************/
  150. node *pos = *head;
  151.  
  152. /*********************************************************
  153. * Walk the list by following the next pointer *
  154. **********************************************************/
  155. while(pos != NULL) {
  156. printf("Source: %.4i Destination: %.4i Type: %.4i Port: %.4i \n", pos->Source, pos->Destination, pos->Type, pos->Port);
  157.  
  158. pos = pos->next ;
  159. }
  160. printf("End of List\n\n");
  161. }
  162.  
  163.  
  164.  
  165. void push(node **head, node **aPacket)
  166. {
  167. /*********************************************************
  168. * Add the cat to the head of the list (*aCat) allows the *
  169. * dereferencing of the pointer to a pointer *
  170. **********************************************************/
  171. (*aPacket)->next = *head;
  172. *head = *aPacket;
  173. }
  174.  
  175. node *pop(node **head)
  176. {
  177. /*********************************************************
  178. * Walk the link list to the last item keeping track of *
  179. * the previous. when you get to the end move the end *
  180. * and spit out the last Cat in the list *
  181. **********************************************************/
  182. node *curr = *head;
  183. node *pos = NULL;
  184. if (curr == NULL)
  185. {
  186. return NULL;
  187. } else {
  188. while (curr->next != NULL)
  189. {
  190. pos = curr;
  191. curr = curr->next;
  192. }
  193. if (pos != NULL) // If there are more cats move the reference
  194. {
  195. pos->next = NULL;
  196. } else { // No Cats left then set the header to NULL (Empty list)
  197. *head = NULL;
  198. }
  199. }
  200. return curr;
  201.  
  202. }
  203.  
  204.  
  205.  
  206.  
  207. void SavePacket(Source, Destination, Type, Port, Data){
  208.  
  209.  
  210.  
  211. FILE *inFile ;
  212. char inFileName[10] = { '\0' } ;
  213.  
  214. printf("Input file name : ") ;
  215. scanf("%s", inFileName) ;
  216.  
  217. //Open file
  218. inFile = fopen(inFileName, "w+");
  219. if (!inFile)
  220. {
  221. fprintf(stderr, "Unable to open file %s", &inFile);
  222. exit(0);
  223.  
  224. }
  225.  
  226. fprintf(inFile, "Source: %i Destination: %i Type: %i Port: %i Data: %s \n", Source, Destination, Type, Port, Data);
  227. fclose(inFile);
  228.  
  229. }
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. void ShowCurrent(){
  238.  
  239.  
  240.  
  241. }
  242.  
  243. void ExitProgramme(){}
  244.  
  245. void Menu(){
  246.  
  247.  
  248. printf("********Welcome****** \n");
  249. printf("Creator Ben Armstrong.\n\n");
  250. printf("*Please Choose an option*\n");
  251. printf("1. Add a new packet\n");
  252. printf("2. Save current packet to file\n");
  253. printf("3. Show current list of packets\n");
  254. printf("4. Exit\n");
  255.  
  256. scanf("%i", &menu);
  257.  
  258. switch(menu)
  259.  
  260. {
  261. case 1:
  262. AddPacket();
  263. break;
  264.  
  265. case 2:
  266. SavePacket();
  267. break;
  268.  
  269. case 3 :
  270. ShowCurrent();
  271. break;
  272.  
  273. case 4 :
  274. ExitProgramme();
  275. break;
  276.  
  277.  
  278.  
  279. }
  280.  
  281.  
  282. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:44:1: error: return type defaults to ‘int’ [-Werror]
prog.c: In function ‘main’:
prog.c:48:1: error: implicit declaration of function ‘Menu’ [-Werror=implicit-function-declaration]
prog.c: In function ‘AddPacket’:
prog.c:91:1: error: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char **’ [-Werror=format]
prog.c:123:1: error: ‘return’ with a value, in function returning void [-Werror]
prog.c:136:1: error: ‘return’ with a value, in function returning void [-Werror]
prog.c:58:5: error: unused variable ‘option’ [-Werror=unused-variable]
prog.c: In function ‘SavePacket’:
prog.c:207:6: error: type of ‘Source’ defaults to ‘int’ [-Werror]
prog.c:207:6: error: type of ‘Destination’ defaults to ‘int’ [-Werror]
prog.c:207:6: error: type of ‘Type’ defaults to ‘int’ [-Werror]
prog.c:207:6: error: type of ‘Port’ defaults to ‘int’ [-Werror]
prog.c:207:6: error: type of ‘Data’ defaults to ‘int’ [-Werror]
prog.c:221:1: error: format ‘%s’ expects argument of type ‘char *’, but argument 3 has type ‘struct FILE **’ [-Werror=format]
prog.c:226:1: error: format ‘%s’ expects argument of type ‘char *’, but argument 7 has type ‘int’ [-Werror=format]
prog.c: At top level:
prog.c:245:6: error: conflicting types for ‘Menu’ [-Werror]
prog.c:48:1: note: previous implicit declaration of ‘Menu’ was here
cc1: all warnings being treated as errors
stdout
Standard output is empty