Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 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[30];
  9. int qtt_stock;
  10. } ARTICLE_TYPE;
  11.  
  12.  
  13. void saisie (ARTICLE_TYPE *p_element) {
  14. printf("ARTICLE");
  15. printf("Entrer le code : ");
  16. scanf("%d", &p_element->code);
  17. printf("Entrer la quantite en stock : ");
  18. scanf("%d", &p_element->qtt_stock);
  19. printf("Entrer le nom : ");
  20. scanf("%s", p_element->nom);
  21. }
  22.  
  23. void affichage (ARTICLE_TYPE element){
  24. printf("\nCode:%d\nQtt:%d\nNom:%s\n", element.code, element.qtt_stock, element.nom);
  25. }
  26.  
  27.  
  28.  
  29. int ouverture (char *nom, int mode, int droits){
  30. int res=open(nom, mode, droits);
  31. return(res);
  32. }
  33.  
  34. int fermeture (int descripteur){
  35. int res=close(descripteur);
  36. return (res);
  37. }
  38.  
  39. int ecriture (int descripteur, ARTICLE_TYPE element){
  40. int res=write(descripteur, &element, sizeof(ARTICLE_TYPE));
  41. return (res);
  42. }
  43.  
  44. int lecture (int descripteur, ARTICLE_TYPE *p_element){
  45. int res=read(descripteur, p_element, sizeof(ARTICLE_TYPE));
  46. return (res);
  47. }
  48.  
  49.  
  50.  
  51.  
  52. long positionner (int des, long pos) {
  53.  
  54. long a, position;
  55. position = sizeof(ARTICLE_TYPE)*pos;
  56.  
  57. a=lseek(des, position, SEEK_SET);
  58. return(a);
  59. }
  60.  
  61.  
  62.  
  63. void main (void){
  64.  
  65. int i, n, v_open;
  66. long position_curseur, numero;
  67. ARTICLE_TYPE aa, art_courant;
  68.  
  69.  
  70. v_open=ouverture("fichier.txt", O_WRONLY, 0666);
  71. printf("Combien d'articles dans le fichier ?");
  72. scanf("%d", &n);
  73. for (i=0; i<n; i++){
  74. saisie(&aa);
  75. ecriture(v_open, aa);
  76. }
  77. close(v_open);
  78.  
  79. v_open=ouverture("fichier.txt", O_RDONLY, 0666);
  80.  
  81. printf("Quel article souhaitez-vous voir? ");
  82. scanf("%ld", &numero);
  83.  
  84. printf("ARTCLE %ld", numero);
  85.  
  86. numero=numero-1;
  87.  
  88. position_curseur=positionner(v_open, numero);
  89.  
  90. lecture(v_open, &art_courant);
  91.  
  92. affichage(art_courant);
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement