Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- typedef struct {
- int code;
- char nom[30];
- int qtt_stock;
- } ARTICLE_TYPE;
- void saisie (ARTICLE_TYPE *p_element) {
- printf("ARTICLE");
- printf("Entrer le code : ");
- scanf("%d", &p_element->code);
- printf("Entrer la quantite en stock : ");
- scanf("%d", &p_element->qtt_stock);
- printf("Entrer le nom : ");
- scanf("%s", p_element->nom);
- }
- void affichage (ARTICLE_TYPE element){
- printf("\nCode:%d\nQtt:%d\nNom:%s\n", element.code, element.qtt_stock, element.nom);
- }
- int ouverture (char *nom, int mode, int droits){
- int res=open(nom, mode, droits);
- return(res);
- }
- int fermeture (int descripteur){
- int res=close(descripteur);
- return (res);
- }
- int ecriture (int descripteur, ARTICLE_TYPE element){
- int res=write(descripteur, &element, sizeof(ARTICLE_TYPE));
- return (res);
- }
- int lecture (int descripteur, ARTICLE_TYPE *p_element){
- int res=read(descripteur, p_element, sizeof(ARTICLE_TYPE));
- return (res);
- }
- long positionner (int des, long pos) {
- long a, position;
- position = sizeof(ARTICLE_TYPE)*pos;
- a=lseek(des, position, SEEK_SET);
- return(a);
- }
- void main (void){
- int i, n, v_open;
- long position_curseur, numero;
- ARTICLE_TYPE aa, art_courant;
- v_open=ouverture("fichier.txt", O_WRONLY, 0666);
- printf("Combien d'articles dans le fichier ?");
- scanf("%d", &n);
- for (i=0; i<n; i++){
- saisie(&aa);
- ecriture(v_open, aa);
- }
- close(v_open);
- v_open=ouverture("fichier.txt", O_RDONLY, 0666);
- printf("Quel article souhaitez-vous voir? ");
- scanf("%ld", &numero);
- printf("ARTCLE %ld", numero);
- numero=numero-1;
- position_curseur=positionner(v_open, numero);
- lecture(v_open, &art_courant);
- affichage(art_courant);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement