#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0

    Question File I/O and linked lists/struct


    I am rather perplexed and stuck with how to implement the following.

    Within a blackjack program, the user can choose to have cards dealt from a pack which is stored on a file cards.txt.

    Having scoured many books/ web-sites etc, I am still struggling with the following problem:-

    How do I read the data from the file?

    The cards are laid out as followed on the file (this is how I have done it anyway!)

    1 A H 1
    2 2 H 2
    ..etc

    (1 is the card number, A is the Ace or 2 the 2 of.. etc, H is the suit and 1, 2 the points value.(I will sort out the value of aces within the program itself.)

    Basically I need to know how to read this data if the user chooses to use the cards.txt pack. Is a linked list preferable here, and is the structure of my .txt file(i.e in columns etc ok for reading data- will the compiler understand it?)

    Please please help a struggling student.

    p.s. I don't want anybody to provide the code for me, just a few pointers(excuse the pun!) in the right direction.

    Thank you
  2. #2
  3. No Profile Picture
    status unknown
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    262
    Rep Power
    12

    Re: File I/O and linked lists/struct


    Originally posted by jpcm24
    Basically I need to know how to read this data if the user chooses to use the cards.txt pack. Is a linked list preferable here, and is the structure of my .txt file(i.e in columns etc ok for reading data- will the compiler understand it?)
    [1] How you read the data is not the same thing as how you store it. I'm not sure whether you're using C or C++ but file I/O is pretty well documented for each, so I assume you know basic file I/O or can understand it from your book? (Otherwise, please ask).

    [2] How you store the data is entirely up to you. For example, you could use something as simple as an array, or you could use a linked list. Personally I think a linked list would be a little overkill for such a simple exercise (unless you're required to use one, or you have something 'off the shelf' that you can adapt).

    [3] Yes, the compiler will understand the structure of your file without any difficulty, it seems pretty straight forward. You just need to write your code accordingly.

    I don't know how good your file I/O is, but I would suggest you make it a goal to go for a solution that is relatively simple to implement (good general advice). If you're not used to file I/O at all, maybe play around with that side of things a little first, then integrate it into your program when you're comfortable with it.

    Choice of language (C or C++) will obviously come into play.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    Some implementation advice:

    1) A deck of cards has a fixed number of members. You should take advantage of this and use an array.

    2) Write the file i/o in two parts. A small function to read in an individual card, and a wrapper function that calls the card reader function for each card in the deck.

    3) Although it's assumed that the deck has a fixed number of cards, take some precautions that your professor might not have you playing with a full deck. Have the card reading function fail gracefully if you run out of cards before you run out of deck.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    Bournemouth(U.K.)
    Posts
    24
    Rep Power
    0
    Thank you very much for the quick replies!

    p.s. I am using C

IMN logo majestic logo threadwatch logo seochat tools logo