Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <unistd.h>
  4.  
  5.  
  6. typedef struct {
  7. int code;
  8. char nom[20];
  9. int qtt_stock;
  10. } ARTICLE_TYPE;
  11.  
  12. void saisie (ARTICLE_TYPE *p_element) {
  13. printf("SAISIR ARTICLE\n");
  14. printf("Entrer le code : ");
  15. scanf("%d", &p_element->code);
  16. printf("Entrer la quantite en stock : ");
  17. scanf("%d", &p_element->qtt_stock);
  18. printf("Entrer le nom : ");
  19. scanf("%s", p_element->nom);
  20. }
  21.  
  22. void affichage (ARTICLE_TYPE element){
  23. printf("\nCode:%d\nQtt:%d\nNom:%s\n", element.code, element.qtt_stock, element.nom);
  24. }
  25.  
  26.  
  27.  
  28. int ouverture (char *nom, int mode, int droits){
  29. int res=open(nom, mode, droits);
  30. return(res);
  31. }
  32.  
  33. int fermeture (int descripteur){
  34. int res=close(descripteur);
  35. return (res);
  36. }
  37.  
  38. int ecriture (int descripteur, ARTICLE_TYPE element){
  39. int res=write(descripteur, &element, sizeof(ARTICLE_TYPE));
  40. return (res);
  41. }
  42.  
  43. int lecture (int descripteur, ARTICLE_TYPE *p_element){
  44. int res=read(descripteur, p_element, sizeof(ARTICLE_TYPE));
  45. return (res);
  46. }
  47.  
  48.  
  49.  
  50. void fusion (int des1, int des2, int des3){
  51. int suiv_fich1 = 1;
  52. int suiv_fich2 = 1;
  53. int fin_fich1 = 0;
  54. int fin_fich2 = 0;
  55. int nb;
  56. ARTICLE_TYPE art1, art2;
  57.  
  58. do {
  59. if ((fin_fich1==0) && (suiv_fich1==1)){
  60. nb = lecture(des1, &art1);
  61. if (nb <=0){
  62. fin_fich1=1;
  63. }
  64. }
  65.  
  66. if ((fin_fich2==0) && (suiv_fich2==1)){
  67. nb = lecture(des2, &art2);
  68. if (nb <=0){
  69. fin_fich2=1;
  70. }
  71. }
  72.  
  73. if ((fin_fich1==0) && (fin_fich2==0)){
  74. if (art1.code < art2.code){
  75. ecriture(des3, art1);
  76. suiv_fich1=1;
  77. suiv_fich2=0;
  78. }
  79. else {
  80. ecriture(des3, art2);
  81. suiv_fich1=0;
  82. suiv_fich2=1;
  83. }
  84. }
  85. else {
  86. if (fin_fich1==1) {
  87. if (fin_fich2==0){
  88. ecriture(des3, art2);
  89. suiv_fich2=1;
  90. }
  91. }
  92. else {
  93. ecriture(des3, art1);
  94. suiv_fich1=1;
  95. }
  96. }
  97. } while ((fin_fich1==0) && (fin_fich2==0));
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104. void main (void){
  105.  
  106. int i, n, m, j, p, k, v_open1, v_open2, v_open3;
  107. ARTICLE_TYPE aa, bb, cc;
  108.  
  109. v_open1=ouverture("donnees1.dat", O_WRONLY | O_CREAT, 0766);
  110. printf("Combien d'articles dans données1 ? ");
  111. scanf("%d", &n);
  112. for (i=0; i<n; i++){
  113. saisie(&aa);
  114. ecriture(v_open1, aa);
  115. }
  116. close(v_open1);
  117.  
  118. v_open1=ouverture("donnees1.dat", O_RDONLY, 0);
  119.  
  120.  
  121. v_open2=ouverture("donnees2.dat", O_WRONLY | O_CREAT, 0766);
  122. printf("Combien d'articles dans données2 ? ");
  123. scanf("%d", &m);
  124. for (j=0; j<m; j++){
  125. saisie(&bb);
  126. ecriture(v_open2, bb);
  127. }
  128. close(v_open2);
  129.  
  130. v_open2=ouverture("donnees2.dat", O_RDONLY, 0);
  131.  
  132.  
  133. v_open3=ouverture("fusion.dat", O_WRONLY | O_CREAT, 0766);
  134. fusion(v_open1, v_open2, v_open3);
  135.  
  136. close(v_open3);
  137. v_open3=ouverture("fusion.dat", O_RDONLY , 0);
  138. p=m+n;
  139. printf("\nVOICI LA LISTE DE TOUS LES ARTICLES");
  140. for (k=0; k<p; k++){
  141. lecture(v_open3, &cc);
  142. printf("\nARTICLE %d", k+1);
  143. affichage(cc);
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement