August 3rd, 2003, 04:41 AM
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
(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.
August 3rd, 2003, 07:58 AM
Re: File I/O and linked lists/struct
 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).
 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).
 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.
August 3rd, 2003, 10:37 AM
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.
Articles and commentary on web development
August 3rd, 2003, 03:56 PM
Thank you very much for the quick replies!
p.s. I am using C