Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4. //Init
  5. struct article_type
  6. {
  7. unsigned int code;
  8. char nom[31];
  9. unsigned long stock;
  10. };
  11.  
  12. void saisie (struct article_type *p_element);
  13. void affichage (struct article_type element);
  14. int ouverture (char *nom, int mode, int droits);
  15. int fermeture (int descripteur);
  16. int ecriture (int descripteur, struct article_type element);
  17. int lecture (int descripteur, struct article_type *p_element);
  18. int positionner (int descripteur, int position);
  19.  
  20. int art_lire;
  21.  
  22. void fusion (int descripteur1, int descripteur2, int descripteur3);
  23.  
  24. //Main
  25. int main (int arg, char *argv[])
  26. {
  27. struct article_type art1;
  28. struct article_type art2;
  29. struct article_type art3;
  30. int nbs_art1=1, nbs_art2=2;
  31. int i;
  32. int doc;
  33.  
  34. int mode = O_RDWR;
  35. int droits = 777;
  36.  
  37. if (arg ==3+1)
  38. {
  39. //ouverture
  40. int fd1=ouverture (argv[1], mode, droits);
  41. //printf("FD : %d\n",fd);
  42. int fd2=ouverture (argv[2], mode, droits);
  43. //printf("FD : %d\n",fd);
  44. int fd3=ouverture (argv[3], mode, droits);
  45. //printf("FD : %d\n",fd);
  46. /*
  47. //init donnée 1
  48. printf("Entrez le nbs d'aricles n :\n");
  49. scanf("%d", &nbs_art1);
  50. for (i=1;i<=nbs_art1;i++)
  51. {
  52. saisie(&art1);
  53. int write1 = ecriture(fd1, art1);
  54. //printf("write = %d\n", write);
  55. }
  56.  
  57. //init données 2
  58. printf("Entrez le nbs d'aricles m :\n");
  59. scanf("%d", &nbs_art2);
  60. for (i=1;i<=nbs_art2;i++)
  61. {
  62. saisie(&art2);
  63. int write2 = ecriture(fd2, art2);
  64. //printf("write = %d\n", write);
  65. }
  66. */
  67. //Fusion
  68. fusion (fd1, fd2, fd3);
  69. //Lecture
  70. printf("Entrez le doc à lire :(1-2-3)\n");
  71. scanf("%d", &doc);
  72. switch (doc)
  73. {
  74. case (1) :
  75. printf("Entrez l'aricles à lire :\n");
  76. scanf("%d", &art_lire);
  77. positionner (fd1, art_lire);
  78. int lect1 = lecture(fd1, &art1);
  79. affichage(art1);
  80. break;
  81. case (2) :
  82. printf("Entrez l'aricles à lire :\n");
  83. scanf("%d", &art_lire);
  84. positionner (fd2, art_lire);
  85. int lect2 = lecture(fd2, &art2);
  86. affichage(art2);
  87. break;
  88. case (3) :
  89. printf("Entrez l'aricles à lire :\n");
  90. scanf("%d", &art_lire);
  91. positionner (fd3, art_lire);
  92. int lect3 = lecture(fd3, &art3);
  93. affichage(art3);
  94. break;
  95. default:
  96. printf("erreur doc :\n");
  97.  
  98. break;
  99. }
  100.  
  101. int ferm1=fermeture(fd1);
  102. int ferm2=fermeture(fd2);
  103. int ferm3=fermeture(fd3);
  104. }
  105. else
  106. {
  107. printf("Entrez 2 fichier de données et 1 fichier a fusionner\n");
  108. }
  109.  
  110.  
  111. }
  112.  
  113. //SP
  114. void saisie (struct article_type *p_element)
  115. {
  116.  
  117. printf("\nSaisie d\'un article\n");
  118. printf("Saisie du code\n");
  119. scanf("%u",&(*p_element).code);
  120. printf("Saisie du nom\n");
  121. scanf("%s",(*p_element).nom);
  122. printf("Saisie du stock\n");
  123. scanf("%lu",&(*p_element).stock);
  124. }
  125.  
  126. void affichage (struct article_type element)
  127. {
  128. printf("\nAffichage d\'un article\n");
  129. printf("Code: %u\n", element.code);
  130. printf("Nom: %s\n", element.nom);
  131. printf("Stock: %lu\n", element.stock);
  132. }
  133.  
  134. int ouverture (char *nom, int mode, int droits)
  135. {
  136. int fd;
  137. fd=open(nom, mode, droits);
  138. return(fd);
  139.  
  140. }
  141.  
  142. int fermeture (int descripteur)
  143. {
  144. return(close(descripteur));
  145. }
  146.  
  147. int ecriture (int descripteur, struct article_type element)
  148. {
  149. return(write(descripteur, &element, sizeof(element)));
  150. }
  151.  
  152. int lecture (int descripteur, struct article_type *p_element)
  153. {
  154. return(read(descripteur, p_element, sizeof(*p_element)));
  155. }
  156.  
  157.  
  158. void fusion (int descripteur1, int descripteur2, int descripteur3)
  159. {
  160. struct article_type art1;
  161. struct article_type art2;
  162. int suivant_fd1, suivant_fd2 = 0;
  163. int fin_fd1, fin_fd2 = 0;
  164. int nb;
  165.  
  166. do
  167. {
  168. if(fin_fd1==0 && suivant_fd1==0)
  169. {
  170. nb=lecture(descripteur1, &art1);
  171. if(nb<=0)
  172. {
  173. fin_fd1=1;
  174. }
  175. }
  176.  
  177. if(fin_fd2==0 && suivant_fd2==0)
  178. {
  179. nb = lecture(descripteur2, &art2);
  180. if (nb<=0)
  181. {
  182. fin_fd2=1;
  183. }
  184. }
  185.  
  186. if(fin_fd1==0 && fin_fd2==0)
  187. {
  188. if(art1.code < art2.code)
  189. {
  190. ecriture(descripteur3, art1);
  191. suivant_fd1=0;
  192. suivant_fd2=1;
  193. }
  194. else
  195. {
  196. ecriture(descripteur3, art2);
  197. suivant_fd1=1;
  198. suivant_fd2=0;
  199. }
  200. }
  201. else
  202. {
  203. if(fin_fd1)
  204. {
  205. if(!fin_fd2)
  206. {
  207. ecriture(descripteur3, art2);
  208. suivant_fd2 = 0;
  209. }
  210. }
  211. else
  212. {
  213. ecriture(descripteur3, art1);
  214. suivant_fd1 = 0;
  215. }
  216. }
  217. }while (fin_fd1 == 0 || fin_fd2 == 0);
  218.  
  219. }
  220.  
  221. int positionner (int descripteur, int position)
  222. {
  223. return(lseek(descripteur,sizeof(struct article_type)*(position-1),0));
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement