Reading Textfile using pointers

solved
Last edited on
Oh, the textfile is in the format: Spade 1 Club 10 Diamond 3 etc...
On line 82 you set wastePtr = wasteHome. This is probably what is erasing your data.
Yeah I forgot I am passing by value so I erased it but still having issues the wastePile array not printing.
The end of the for loop execution on lines 214-216 in your post leave wastePtr pointing to the end of the wastePile.

Then when you go to print you continue to increase the wastePtr past its bounds.
Last edited on
Yeah I believe I was able to rectify that by resetting the pointer before it is printed. But for some reason, everything is working except it is not writing the wastePile to the file but the tableau just fine. Any reason what would cause that?
Did you also reset it before you call writeCards?

Don't forget to make these changes for case 'p' too.
Last edited on
I just editted my original post with the latest changes. So everything is working except when I tried to read in a previous game, says segmentatio fault. and its not writing to the previousGame texfile.
Now that wasteHome points to where the waste pile begins, you should set wastePtr = wasteHome before you call printWastePile and writeCards. Let me know if this fixes it.
Okay so that did help quite a bit, there is still some garbage getting spit out at the end. But the weird thing is that it doesn't write the wastePile to previousGame.txt
Okay up until now I had not looked at the writeCards function very closely.
But looking at it I can tell that counter variable goes from 0 to 6 every time.
And the else if on line 257 might need to be a regular if?
I am not altogether sure on your logic here, but I do know that line will never execute.
1
2
3
4
5
6
7
8
9
int counter = 0;
if( counter < 28 )
{
    //I will do stuff :)
}
else if( counter < 52 )
{
    //I will never do stuff :(
}

My point is the else if is sad. It will never be able to execute its code :(
Yeah that did fix the write issue. Thanks for all the help. My logic was that after counter > 28 it would hit the else if statement. As far as printing out the wastePile, for some reason it outputs garbage at the end. Anyway, I really do appreciate all your help, I have banging my head on this last 3 days.
It is still printing garbage at the end of printing out the wastePile to the screen?
Could you edit your code post again? I will run it on my machine to see what it is doing.
Last edited on
I edited my original post with the latest.

Here's the text inside "cards.txt"


Heart 1 Diamond 2 Heart 12 Diamond 12 Heart 11 Heart 10 Club 2 Heart 9 Club 7 Heart 7
Heart 6 Heart 5 Spade 12 Spade 13 Club 4 Spade 11 Spade 10 Spade 9 Spade 8 Diamond 1 Spade 6 Spade 5 Spade 3 Spade 2 Club 1 Club 13 Club 11 Heart 4 Heart 2 Club 10 Spade 1 Club 9 Diamond 4 Club 8 Diamond 3 Club 6 Heart 13 Diamond 9 Club 5 Club 3 Diamond 13 Diamond 11 Spade 7 Diamond 10 Diamond 8 Spade 4 Heart 8 Diamond 7 Heart 3 Diamond 6 Club 12 Diamond 5
Last edited on
I ran the code and did not see any garbage as far as I can tell on the screen.
Now in the file there is just an extra space at the end, but it printed off everything.



C:\Users\Kevin\Desktop\cplusplus.com\Reading Textfile using pointers>g++ cards.cpp

C:\Users\Kevin\Desktop\cplusplus.com\Reading Textfile using pointers>a
Enter filename to start game
cards.txt



Main Menu
---------
1. <N>ew Game
2. <P>revious Game
3. <Q>uit Game
4. ENTER SELECTION

n

Heart 1 Diamond 2 Heart 12 Diamond 12 Heart 11 Heart 10 Club 2
          Heart 9 Club 7 Heart 7 Heart 6 Heart 5 Spade 12
                  Spade 13 Club 4 Spade 11 Spade 10 Spade 9
                          Spade 8 Diamond 1 Spade 6 Spade 5
                                  Spade 3 Spade 2 Club 1
                                          Club 13 Club 11
                                                  Heart 4
Waste Pile
----------
Heart 2 Club 10 Spade 1 Club 9 Diamond 4 Club 8 Diamond 3 Club 6 Heart 13 Diamond 9 Club 5 Club 3 Dia
mond 13 Diamond 11 Spade 7 Diamond 10 Diamond 8 Spade 4 Heart 8 Diamond 7 Heart 3 Diamond 6 Club 12 D
iamond 5

Main Menu
---------
1. <N>ew Game
2. <P>revious Game
3. <Q>uit Game
4. ENTER SELECTION

q



C:\Users\Kevin\Desktop\cplusplus.com\Reading Textfile using pointers>a
Enter filename to start game
cards.txt



Main Menu
---------
1. <N>ew Game
2. <P>revious Game
3. <Q>uit Game
4. ENTER SELECTION

n

Heart 1 Diamond 2 Heart 12 Diamond 12 Heart 11 Heart 10 Club 2
          Heart 9 Club 7 Heart 7 Heart 6 Heart 5 Spade 12
                  Spade 13 Club 4 Spade 11 Spade 10 Spade 9
                          Spade 8 Diamond 1 Spade 6 Spade 5
                                  Spade 3 Spade 2 Club 1
                                          Club 13 Club 11
                                                  Heart 4
Waste Pile
----------
Heart 2 Club 10 Spade 1 Club 9 Diamond 4 Club 8 Diamond 3 Club 6 Heart 13 Diamond 9 Club 5 Club 3 Dia
mond 13 Diamond 11 Spade 7 Diamond 10 Diamond 8 Spade 4 Heart 8 Diamond 7 Heart 3 Diamond 6 Club 12 D
iamond 5

Main Menu
---------
1. <N>ew Game
2. <P>revious Game
3. <Q>uit Game
4. ENTER SELECTION

p

Enter previous game
cards.txt
Heart 1 Diamond 2 Heart 12 Diamond 12 Heart 11 Heart 10 Club 2
          Heart 9 Club 7 Heart 7 Heart 6 Heart 5 Spade 12
                  Spade 13 Club 4 Spade 11 Spade 10 Spade 9
                          Spade 8 Diamond 1 Spade 6 Spade 5
                                  Spade 3 Spade 2 Club 1
                                          Club 13 Club 11
                                                  Heart 4
Waste Pile
----------
Heart 2 Club 10 Spade 1 Club 9 Diamond 4 Club 8 Diamond 3 Club 6 Heart 13 Diamond 9 Club 5 Club 3 Dia
mond 13 Diamond 11 Spade 7 Diamond 10 Diamond 8 Spade 4 Heart 8 Diamond 7 Heart 3 Diamond 6 Club 12 D
iamond 5

Main Menu
---------
1. <N>ew Game
2. <P>revious Game
3. <Q>uit Game
4. ENTER SELECTION

q



C:\Users\Kevin\Desktop\cplusplus.com\Reading Textfile using pointers>

Hmm thats very interesting, I'll try making a new text file. Once again thanks for all your help!
Topic archived. No new replies allowed.