Posted: Sat Jan 19, 2013 8:21 pm Post subject: I'm trying to add lines into the middle of a text file?
I'm using Dev-C++
The program I'm working on requires me to output 75,000 poker hands subheadings (bust,pair,three of a kind, straight, flush, etc.) of various types of hands.
Each hand consists of 5 cards and I have a datatype for each player that stores the 5 cards that player has.
The output will be like this:
Royal Flush
Hand 34321
Jack of Spades
Queen of Spades
Ten of Spades
King of Spades
The problem I'm having is that I cannot store 75000 hands (375,000 cards) without the program crashing.
The one way I think I can do this is by parsing and assigning each hand to a subheading and saving that to an output file.
For example: I parse the first hand and analyze it and it turns out to be a straight. So then I go under the straights subheading in my output file, add this hand under that subheading, and close the file. Then I go to the second hand and do the same thing - since I'm never storing more than one hand at a time, crashing won't be a problem.
The one problem I have though is that I don't know how I can add lines into the middle of a text file in the place I want.
I've looked into fopen but I don't think there's anything there that allows me to add to a text file unless it's the end of the text file.
Any help or other ideas of going about this assignment would be very much appreciated.
Sponsor Sponsor
Insectoid
Posted: Sat Jan 19, 2013 8:31 pm Post subject: RE:I\'m trying to add lines into the middle of a text file?
I've got to wonder why you want to store every hand like this. There isn't really a reason for it.
canmovie
Posted: Sat Jan 19, 2013 8:34 pm Post subject: Re: RE:I\'m trying to add lines into the middle of a text file?
Insectoid @ Sat Jan 19, 2013 8:31 pm wrote:
I've got to wonder why you want to store every hand like this. There isn't really a reason for it.
The reason I have to store each hand in an output file is because that's what my teacher wants.
Can you extrapolate on what you mean by "like this"?
I HAVE NO OTHER WAYS OF GOING ABOUT THIS - That's why I'm posting here.
Your criticism is not helping because it pure criticism - you're not even telling me what you're criticisizing.
Also, I ended with : "Any help or other ideas of going about this assignment would be very much appreciated."
Your post is neither helping, or providing other ways of doing this, and therefore, IT IS NOT APPRECIATED.
Insectoid
Posted: Sat Jan 19, 2013 8:46 pm Post subject: RE:I\'m trying to add lines into the middle of a text file?
Okay, if your teacher requires it then that's a good reason.
You can't add lines in the middle of a text file. What order do you want to store the hands in? Generate them in that order.
canmovie
Posted: Sat Jan 19, 2013 8:54 pm Post subject: Re: I'm trying to add lines into the middle of a text file?
Insectoid @ Sat Jan 19, 2013 8:46 pm wrote:
Okay, if your teacher requires it then that's a good reason.
You can't add lines in the middle of a text file. What order do you want to store the hands in? Generate them in that order.
THIS post is appreciated.
I'm not generating the hands - they're given to me in the form of a text file.
So far my program parses the hands and stores them in memory.
The problem is that when the number of hands passes a few thousand, the program crashes.
The finished program has to read 75,000 poker hands from a file, and make another file consisting of the same hands, but this time sorted under specific categories (bust,pair, three of a kind,etc)
Tony
Posted: Sat Jan 19, 2013 8:56 pm Post subject: RE:I\'m trying to add lines into the middle of a text file?
You did not make it clear why your program is crashing. You seem to be alluding towards an out-of-memory exceptions, but one can easily fit 75000 integers even on tiny embedded devices.
The necessity to store _all_ of those hands in memory, at the same time, is still questionable though.
Posted: Sat Jan 19, 2013 9:20 pm Post subject: RE:I\'m trying to add lines into the middle of a text file?
To add lines to the middle of a text file you would need to save all of the data past the point you want to put the new data in, in an array, and then delete it, and then print the new data + old data, i think this is the only way though
canmovie
Posted: Sat Jan 19, 2013 9:29 pm Post subject: Re: I'm trying to add lines into the middle of a text file?
Quote:
ICS4U Final Project
There are 2 parts to your final project. You will be writing 2 separate but related programs.
Part One
You have been given two text files containing randomly generated 5-card poker hands. One of the files contains 1000 hands while the other file contains 75000 hands. Your program should be able to handle both of these input files.
Read these files into memory and output (to two separate files) the hands sorted by the type of each hand(bust, one pair, two pair, three of a kind, straight, flush, full house, straight flush, royal flush). Output to a separate file the frequency distribution table for the number of hands of each type. You will have 2 separate frequency distribution tables ? one based on the file containing 1000 hands and one based of the file containing 75000 hands.
Output Requirements
Output should look exactly as below:
Bust Hands
==========
Hand #1
Eight of Diamonds
Four of Clubs
Nine of Spades
King of Diamonds
Ace of Spades
Hand #2
Seven of Spades
Ace of Hearts
Ten of Clubs
Four of Spades
Three of Clubs
Hand #3
Jack of Diamonds
Ace of Spades
Seven of Clubs
Six of Spades
Five of Hearts
Hand #4
Four of Diamonds
Six of Hearts
Eight of Spades
Three of Clubs
Queen of Spades
?
One Pair Hands
=============
Hand #5
Four of Diamonds
Six of Hearts
Queen of Spades
Three of Clubs
Queen of Spades
Hand #6
Seven of Diamonds
Six of Clubs
Jack of Spades
Nine of Hearts
Six of Spades
?
Notice that the hands are formatted the same way as they were in the input file.
Those are the requirements. Can anyone tell me how I can do this if I can't store all 75,000 hands?[/quote]
Sponsor Sponsor
Tony
Posted: Sat Jan 19, 2013 9:30 pm Post subject: RE:I\'m trying to add lines into the middle of a text file?
Right, but the problem is that this performance is really expensive.
OP would need to think of a better approach where it's not necessary to reshuffle the same file so many times.
Posted: Sat Jan 19, 2013 10:08 pm Post subject: Re: RE:I\'m trying to add lines into the middle of a text file?
Tony @ Sat Jan 19, 2013 9:30 pm wrote:
Right, but the problem is that this performance is really expensive.
OP would need to think of a better approach where it's not necessary to reshuffle the same file so many times.
I thought there might be an obvious solution that I might be missing, but have now found out that's not the case, thanks anyways.
md
Posted: Sat Jan 19, 2013 11:43 pm Post subject: RE:I\'m trying to add lines into the middle of a text file?
The obvious solution is that you're doing something wrong with your in-memory data structures. There is absolutely no reason why you should be running out or memory.
As has been mentioned, inserting text into the middle of a file requires storing all of the data between the insertion point and the end of the file. Since you're already running into memory issues I doubt that you'll be able to make use of a scratch file effectively.
canmovie
Posted: Sun Jan 20, 2013 12:07 am Post subject: Re: RE:I\'m trying to add lines into the middle of a text file?
md @ Sat Jan 19, 2013 11:43 pm wrote:
The obvious solution is that you're doing something wrong with your in-memory data structures. There is absolutely no reason why you should be running out or memory.
As has been mentioned, inserting text into the middle of a file requires storing all of the data between the insertion point and the end of the file. Since you're already running into memory issues I doubt that you'll be able to make use of a scratch file effectively.
There is nothing wrong with my in-memory data structures:
code:
typedef struct
{
int value;
int suit;
char name[17];
} cardType;
I can store up to 10,000 hands without a problem but when I get into larger numbers I run the program and the windows error message comes up : blahblah.exe has stopped working, windows is checking for a solution to the problem...
I've realized that I don't need the name of every card stored as a string, I can just parse it when I need to but even without it my program crashes. Everything else I'm storing is essential.
md @ Sat Jan 19, 2013 11:43 pm wrote:
There is absolutely no reason why you should be running out or memory.
If this is true, then why is my program crashing when I up the number of hands?
wtd
Posted: Sun Jan 20, 2013 12:20 am Post subject: RE:I\'m trying to add lines into the middle of a text file?
Have you considered posting some code so we can see where your problems might be?
Also, char arrays and typedefed structs? Is this supposed to be C++ or C?
canmovie
Posted: Sun Jan 20, 2013 1:30 am Post subject: Re: RE:I\'m trying to add lines into the middle of a text file?
wtd @ Sun Jan 20, 2013 12:20 am wrote:
Have you considered posting some code so we can see where your problems might be?
Also, char arrays and typedefed structs? Is this supposed to be C++ or C?
The following function is the function that reads every line in the .txt file and sends the strings that contain cards into parsing functions, which, in return, pass back values of 1-13 (value of card) or 1-4 (suit of card).
This function also returns the total number of hands read from the file (max).
The problem is that when I call this function, and the number of elements in the array of handType exceed ~20,000 (so 20,000 hands) the program crashes.
Would the program work if instead of passing one huge array of handType, I passed 5 small ones each containing 15,000 hands for the 75,000 hands I need to store? This came to my mind as I typed this out.
Maybe it's not a memory problem, it's just a limit on the elements of one array?
Actually, that may be it. Because even if I increase the elements of the array to 20,000 but have a .txt. file that only has 5 cards in it, the program still crashes.
Is this because the program is trying to reserve enough space for 20,000 hands to fit in, and cannot find that space, or is this because the array has a maximum element limit?
Whoa, nevermind I don't even need to paste the code, it's DEFINITELY a memory problem.
The computer cannot store:
75,000 Hands = 5 ints 1-13, 5 ints 1-4 for each hand = 375,000 + 375,000 = 750,000 ints
I can get to a maximum of 25,000 hands by declaring the integers representing the values and suits as unsigned.
Is there another way of making integers take up even less space?
AHA YES there is I declared the values as chars not as ints and that solved the issue, since no number is bigger than 1 byte or negative, I declared suit and value as unsigned char, and now I can store up to 100,000 hands.
Can't believe you guys couldn't spot it, what a stupid little detail.
Posted: Sun Jan 20, 2013 1:41 am Post subject: RE:I\'m trying to add lines into the middle of a text file?
Each card can be represented by a value in the range 0-53 (8 bits). You can represent an entire hand in 5 bytes. Then if you allocate even 1MB of memory, you can store nearly 210,000 hands.
Even if you do store 75,000 hands at 10 ints = 40 bytes (assuming 32-bit integer, though that's not guaranteed with C/C++), that's still only 3MB of memory. Your computer probably has over 1000 times that.
Even your 'original' struct is only 25 bytes per card, which is less than 9MB of storage total.
I seriously don't think you're running out of memory.
There are a lot of other things that could cause your program to crash with the larger data set. Are you allocating any arrays of fixed size? Are you using any for loops over fixed ranges? Are you leaking heap-allocated strings? Are you multiple-delete or multiple-freeing? We can't help you answer that question without your full source code.