Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define SIZE 10000
  5.  
  6. int TestIfFileExists(char *FileName);
  7. void meny(int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp);
  8. struct RegisterNewProduct(storage s[],int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp);
  9.  
  10. struct storage
  11. {
  12. int ProductId;
  13. char ProductName[30];
  14. int StorageBalance;
  15.  
  16. };
  17. //typedef struct storage Storage;
  18.  
  19. int TestIfFileExists(char *FileName);
  20.  
  21. int main()
  22.  
  23. {
  24. int ProductId, StorageBalance;
  25. char ProductName[20];
  26. struct storage s[SIZE];
  27. //int size = sizeof(s)/sizeof(s[0]);
  28. char FileName[100];
  29. printf("Enter filename (xxx.txt): ");
  30. scanf("%s", FileName);
  31. FILE *fp;
  32. int TrueFalse = TestIfFileExists(FileName);
  33. int i = 0;
  34. int AmountToScan = 0;
  35. int n = 0;
  36. char Buffer[100];
  37.  
  38. if (TrueFalse)
  39. {
  40.  
  41. printf("Reading existing file: %s\n", FileName);
  42. fp = fopen(FileName, "r+");
  43.  
  44. while (!feof(fp))
  45. {
  46. fgets(Buffer, 100, fp);
  47.  
  48. AmountToScan++;
  49.  
  50. }
  51.  
  52. fclose(fp);
  53.  
  54. fp = fopen(FileName, "r+");
  55.  
  56. AmountToScan--;
  57.  
  58.  
  59. while (i != AmountToScan)
  60. {
  61. fscanf(fp, "%d", &s[i].ProductId);
  62. fscanf(fp, "%s", s[i].ProductName);
  63. fscanf(fp, "%d", &s[i].StorageBalance);
  64.  
  65. printf("The product id is: %d\n", s[i].ProductId);
  66. printf("The product name is: %s\n", s[i].ProductName);
  67. printf("The storage balance is: %d\n", s[i].StorageBalance);
  68. n++;
  69. i++;
  70. }
  71.  
  72. fclose(fp);
  73. fp = fopen(FileName, "a");
  74. meny(ProductId, ProductName, StorageBalance, AmountToScan, fp);
  75. fclose(fp);
  76.  
  77.  
  78. }
  79.  
  80. if (!TrueFalse)
  81. {
  82. printf("Creating new file: %s \n", FileName);
  83. fp = fopen(FileName, "w");
  84.  
  85. struct storage s[SIZE] = { 0 };
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92. }
  93.  
  94. int TestIfFileExists(char *FileName)
  95. {
  96. FILE *fp;
  97. if (fp = fopen(FileName, "r"))
  98. {
  99. fclose(fp);
  100. return 1;
  101. }
  102. else
  103. {
  104. return 0;
  105. }
  106.  
  107. }
  108. void meny(int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp)
  109. {
  110. int MenyChoise;
  111. int i = 0;
  112.  
  113. do
  114. {
  115. printf("Would you like to:\nRegister a new product(1)?\nPrint out all products(2)?\nSearch for Product(3)?\nChange storage balance(4)?\nSort products(5)?\nUnregister Product(6)?\nTerminate program(0)?\n");
  116. printf("Enter your choise: ");
  117. scanf("%d",&MenyChoise);
  118.  
  119. if (MenyChoise == 1)
  120. {
  121. RegisterNewProduct(ProductId, ProductName, StorageBalance, AmountToScan, fp);
  122. }
  123. }
  124. while(MenyChoise != 0);
  125. }
  126.  
  127.  
  128. struct RegisterNewProduct(storage s[],int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp)
  129. {
  130.  
  131. fprintf(fp,"%d", s[0].ProductId);
  132. printf("%d",s[0].ProductId);
  133. int i;
  134.  
  135. {
  136. i = ++AmountToScan;
  137. AmountToScan--;
  138. printf("%d\n",i);
  139.  
  140. printf("Enter the product id: ");
  141. scanf("%d",&s[i].ProductId);
  142. for (i=0; i < AmountToScan; i++)
  143. {
  144. if ((s[AmountToScan].ProductId) != (s[i].ProductId))
  145. {
  146. fprintf(fp,"%d", s[i].ProductId);
  147. printf("Enter the product name is: ");
  148. scanf("%s",&s[i].ProductName);
  149. fprintf(fp," %s", s[i].ProductName);
  150. printf("Enter the storage balance: ");
  151. scanf("%d",&s[i].StorageBalance);
  152. fprintf(fp," %d\n", s[i].StorageBalance);
  153.  
  154. }
  155. else
  156. {
  157. printf("error!\n");
  158. break;
  159. }
  160. }
  161.  
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement