View Single Post
  #1 (permalink)  
Old November 21st, 2006, 09:44 PM
fadiyounes1 fadiyounes1 is offline
Registered User
 
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default C programming question

hi,
i am currently trying to modify my program to be able to allow a user to edit the amount of seats available, at the moment i can get my program to decrement the amount of seats, but i am trying to get it to allow a user to modify the amount of seats. i have found it quite difficult to do this, any help would be appreciated.
thank you


   /* Sell seat program -- no record locking */
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>


    main(int argc, char *argv[])
    {
    int train, seats, fd;

    /* open the seat file in read/write mode */
    fd = open(argv[1], O_RDWR);
    if (fd<0) {
        perror("cannot open seat file");
        exit(2);
    }

    /* check a flight number entered */
    if (argc != 2) {
        printf("usage: %s train_number\n", argv[0]);
        exit(1);
    }

    /* convert flight number to an integer */
    train = atoi(argv[1]);

    /* use lseek to move the pointer to the position in the file */
    /* containing the data for the corresponding flight */
    /* lseek takes as args the file descriptor of the file, an */
    /* offset value, which is here calculated by the number of */
    /* elements multiplied by the size of an element */
    /* and an origin point, here defined using SEEK_SET, ie the */
    /* current file position (the start in this case because it */
    /* has just been opened */
    /* NB lseek doesn't do any reading or writing */
    lseek(fd, train*sizeof(int), SEEK_SET);

    /* now read an integer from the file */
    read (fd, &seats, sizeof(int));

    /* decrement it to simulate a sale of a seat */
    seats -- ;
    sleep(1); /* To increase likelihood of a problem */
    printf("%d seats remaining on train %d\n",
        seats, train);

    /* use lseek to move back one place, because the write */
    /* moved it on, and we want to rewrite the element we */
    /* just read */
    lseek(fd, (off_t)(train*sizeof(int)), SEEK_SET);

    /* now write the decremented value back to the file */
    write(fd, &seats, sizeof(int));

    /* done, close the file */
    close(fd);
    }



Reply With Quote