Wrox Programmer Forums
Other Programming Languages If you have a coding issue to discuss about another language that really isn't provided for in any other forum here (not ASP.NET C#, C++, VB, PHP, JavaScript, Python, Java, Perl, Applescript, XML or any of the other forum topics we have), post it here. Enough discussion on a language we don't have covered could prompt a new forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Other Programming Languages section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 21st, 2006, 09:44 PM
Registered User
Join Date: Nov 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default C programming question

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");

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

    /* 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 */

Reply With Quote
  #2 (permalink)  
Old February 6th, 2007, 10:46 AM
Friend of Wrox
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts

One approach could be to get them to specify the number of seats on the command line. I.e.

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

    /* convert flight number to an integer */
    train = atoi(argv[1]);
    seat  = atoi(argv[2]);
    /* etc... */
Obviously if you want it to be interactive that's a little trickier. Ask the user to enter the new number of seats after this line:
    printf("%d seats remaining on train %d\n",
        seats, train);
And then write the user input number of seats to your file rather than the decremented one.

Hope that Helps,

Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk
Reply With Quote

Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a question re: Oject Oriented Programming dotnetDeveloper General .NET 2 July 24th, 2008 05:33 PM
device DLL programming in client side programming hendyhanusin ASP.NET 1.0 and 1.1 Basics 0 March 21st, 2007 08:05 AM
Student Programming question Rob8oD C# 1 February 26th, 2007 07:24 AM
Beginner SQL Programming Question monocles303 SQL Language 1 September 12th, 2006 03:47 AM
Java Server Programming book question xyoon All Other Wrox Books 0 September 8th, 2003 01:45 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.