I have no clue why this is happening. I have a struct that gets printed to a file perfectly fine, but when I try to read out that file I get something not even close to what is actually there. The write/read portion:
int save_data(){
char enter = 0;
int response;
while(true){
printf("1. Add new player\n2. View player data\n3. Return to main");
printf("\nSelection: ");
scanf("%d", &response);
if (response == 1){
FILE* PlayerFile = fopen("players.txt","w");
int i = 0;
for (i = 0; i < 1; i++){
struct player_info aPlayer = create_player();
fprintf(PlayerFile, "Name: %s\nLevel: %d\nStrength Mod: %d\nDexterity Mod: %d\nConstitution Mod: %d\nIntelligence Mod: %d\nWisdom Mod: %d\nCharisma Mod: %d\n", aPlayer.name, aPlayer.Level, aPlayer.Str, aPlayer.Dex, aPlayer.Con, aPlayer.Int, aPlayer.Wis, aPlayer.Cha);
}
fclose(PlayerFile);
return 0;
}
else if (response == 2){
struct player_info aPlayer;{
char name[30];
int Level, Str, Dex, Con, Int, Wis, Cha;
};
FILE* PlayerFile = fopen("players.txt","r");
for (int i = 0; i < 1; i++){
struct player_info create_player;
fscanf(PlayerFile, "Name: %s\nLevel: %d\nStrength Mod: %d\nDexterity Mod: %d\nConstitution Mod: %d\nIntelligence Mod: %d\nWisdom Mod: %d\nCharisma Mod: %d\n", aPlayer.name, &aPlayer.Level, &aPlayer.Str, &aPlayer.Dex, &aPlayer.Con, &aPlayer.Int, &aPlayer.Wis, &aPlayer.Cha);
printf("\nName: %s\nLevel: %d\nStr mod: %d\nDex mod: %d\nCon mod: %d\nInt mod: %d\nWis mod: %d\nCha mod: %d\n\n", aPlayer.name, &aPlayer.Level, &aPlayer.Str, &aPlayer.Dex, &aPlayer.Con, &aPlayer.Int, &aPlayer.Wis, &aPlayer.Cha);
}
fclose(PlayerFile);
}
else if (response == 3){
false;
break;}
}
}
The input that is actually in the file:
Name: Hamfast Drummond
Level: 9
Strength Mod: 8
Dexterity Mod: 7
Constitution Mod: 6
Intelligence Mod: 5
Wisdom Mod: 4
Charisma Mod: 3
The result when printing the above:
Name: Hamfast
Level: 6422172
Str mod: 6422176
Dex mod: 6422180
Con mod: 6422184
Int mod: 6422188
Wis mod: 6422192
Cha mod: 6422196
Now, I realize that the one line is missing the &
before all the variables, but when I add that in what it prints to the file is the same wrong numbers and reading out is correct in that case, but It's not printing to the file what I put in.
As a side question, if I'm allowed two in one, I'd also like it to print the full name rather than just the first, but I'm not sure how to do that.