我是一个初学者,我在学校的一个项目上遇到了麻烦。我的程序大纲运行得很好,但我在保存新数据到我的球员.txt文件上遇到了麻烦。我遇到的问题是如何将新数据保存到我的 players.txt 文件中。
如果我和一个玩家玩游戏,他们赢了10分,我想保存到.txt文件中,并在我查看前5名的余额时显示出来,或者如果我关闭程序并打开它,再和该玩家一起玩。
目前它只保存赢钱输钱的数据,而我停留在play()函数中。然后它就会恢复到 players.txt 文件中的原始数字。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char Name[20];
int Balance;
int Gain;
} Person;
Person player[10];
//Function Declaration to get data for ingame
Person GetPlayerData(const char* name, Person *p);
//Function Declaration to exit to main menu when finished with game
void mainMenu(void);
//Opens file player.txt and puts it into array of persons: Person player[10]
void loadPlayer()
{
//initialize an array for players with room for 10 player's information
//opens file with pointer fptr
FILE *fptr;
fptr = fopen("players.txt", "r");
//prints error if file doesn't exist
if(fptr == NULL)
{
printf("Error loading player.txt\n");
}
//Runs through each line of the file, assigning name, balance and gain to the array player[]
int i=0;
while (fscanf(fptr, "%s%d%d", player[i].Name, &player[i].Balance, &player[i].Gain) != EOF)
{
i++;
}
for(i=0;i<10;i++)
{
printf("%s\t%d\t%d\n", player[i].Name, player[i].Balance, player[i].Gain);
}
fclose(fptr);
}
//GetPlayerData function
Person GetPlayerData(const char* name, Person *p) {
int j = 0;
for (;;) {
if (strcmp(name, p[j].Name) == 0) {
return p[j];
}
j++;
}
}
//Play function
void play(void) {
char name[20];
int bal = 0;
int dice1, dice2, sumofDice;
printf("Enter your name: ");
scanf("%s", &name);
Person playerDetails = GetPlayerData(name, player);
getchar();
//Begin Game
while (1) {
printf("Press Enter to Roll the dice.\n");
getchar();
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
sumofDice = dice1 + dice2;
printf("The values of the dices are %d and %d whose sum is %d.\n", dice1, dice2, sumofDice);
if (sumofDice == 7 || sumofDice == 11) {
playerDetails.Balance = playerDetails.Balance + 10;
printf("You win the game. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
break;
}
}
else if (sumofDice == 2 || sumofDice == 3 || sumofDice == 12) {
playerDetails.Balance = playerDetails.Balance - 1;
printf("You lose. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
getchar();
break;
}
}
else {
int point = sumofDice;
//Continuing the game after your point is declared
while (1) {
printf("Press Enter to Roll the dice.\n");
getchar();
int dice1 = rand() % 6 + 1;
int dice2 = rand() % 6 + 1;
int sumofDice = dice1 + dice2;
printf("The values of the dice are %d and %d whose sum is %d.\n", dice1, dice2,
sumofDice);
if (sumofDice == point) {
playerDetails.Balance = playerDetails.Balance + 10;
printf("You win the game. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
break;
}
}
else if (sumofDice == 7) {
playerDetails.Balance = playerDetails.Balance - 1;
printf("You lose. Your current balance is %d.\n", playerDetails.Balance);
char userChoice;
printf("Play another game? y/n ");
scanf("%s", &userChoice);
if (userChoice == 'y') {
getchar();
continue;
}
else {
break;
}
}
else {
continue;
}
}
break;
}
}
getchar();
mainMenu();
}
void topBalance()
{
int firstLoc, i;
int firstVal = 0;
for(i=0; i<10; i++)
{
if ((player[i].Balance) > firstVal)
{
firstVal = player[i].Balance;
firstLoc = i;
}
}
int secondLoc;
int secondVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > secondVal) && ((player[i].Balance) < firstVal))
{
secondVal = player[i].Balance;
secondLoc = i;
}
}
int thirdLoc;
int thirdVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > thirdVal) && ((player[i].Balance) < secondVal))
{
thirdVal = player[i].Balance;
thirdLoc = i;
}
}
int forthLoc;
int forthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > forthVal) && ((player[i].Balance) < thirdVal))
{
forthVal = player[i].Balance;
forthLoc = i;
}
}
int fifthLoc;
int fifthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Balance) > fifthVal) && ((player[i].Balance) < forthVal))
{
fifthVal = player[i].Balance;
fifthLoc = i;
}
}
printf("1st Place %s \t%d\n", player[firstLoc].Name, player[firstLoc].Balance);
printf("2nd Place %s \t%d\n", player[secondLoc].Name, player[secondLoc].Balance);
printf("3rd Place %s \t%d\n", player[thirdLoc].Name, player[thirdLoc].Balance);
printf("4th Place %s \t%d\n", player[forthLoc].Name, player[forthLoc].Balance);
printf("5th Place %s \t%d\n", player[fifthLoc].Name, player[fifthLoc].Balance);
}
void topGain()
{
int firstLoc, i;
int firstVal = 0;
for(i=0; i<10; i++)
{
if ((player[i].Gain) > firstVal)
{
firstVal = player[i].Gain;
firstLoc = i;
}
}
int secondLoc;
int secondVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > secondVal) && ((player[i].Gain) < firstVal))
{
secondVal = player[i].Gain;
secondLoc = i;
}
}
int thirdLoc;
int thirdVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > thirdVal) && ((player[i].Gain) < secondVal))
{
thirdVal = player[i].Gain;
thirdLoc = i;
}
}
int forthLoc;
int forthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > forthVal) && ((player[i].Gain) < thirdVal))
{
forthVal = player[i].Gain;
forthLoc = i;
}
}
int fifthLoc;
int fifthVal = 0;
for(i=0;i<10;i++)
{
if (((player[i].Gain) > fifthVal) && ((player[i].Gain) < forthVal))
{
fifthVal = player[i].Gain;
fifthLoc = i;
}
}
printf("1st Place %s \t%d\n", player[firstLoc].Name, player[firstLoc].Gain);
printf("2nd Place %s \t%d\n", player[secondLoc].Name, player[secondLoc].Gain);
printf("3rd Place %s \t%d\n", player[thirdLoc].Name, player[thirdLoc].Gain);
printf("4th Place %s \t%d\n", player[forthLoc].Name, player[forthLoc].Gain);
printf("5th Place %s \t%d\n", player[fifthLoc].Name, player[fifthLoc].Gain);
}
void mainMenu()
{
int menuSellect;
printf("\n--------Main Menu--------");
printf("\n0) Top Up Account");
printf("\n1) Play");
printf("\n2) Top 5 Players by Balance");
printf("\n3) Top 5 Players by Winnings");
printf("\n4) Exit Game");
printf("\n-------------------------\n");
//prompt user to pick a menu item 0 to 3
printf("Please Enter a Number for a Valid Menu Item: ");
scanf("%d", &menuSellect);
printf("\n\n");
//switch statement controlls what menu item is going to be sellected
switch (menuSellect)
{
case 0:
printf("Top Up Account\n");
mainMenu();
break;
case 1:
printf("Play\n");
play();
mainMenu();
break;
case 2:
printf("Top 5 Players by Balance\n");
topBalance();
mainMenu();
break;
case 3:
printf("Top 5 Players by Winnings\n");
topGain();
mainMenu();
break;
case 4:
printf("Exitting Game\n");
exit(0);
//deffault catches any errors and prints the main menu a second time
deffault:
printf("Invalid Menu Option\n");
mainMenu();
break;
}
}
void main()
{
loadPlayer();
mainMenu();
}
通过这段代码,我可以看到你打开 "player.txt "文件,并使用fscanf将数据读到你的程序中。你缺少的是将这些数据再次写回的代码,无论是在你退出时还是在游戏完成后。调查打开文件进行写入,然后使用类似fprintf的东西来保存数据。
注意,你是用 "r "来打开文件进行读取。当你写出来的时候,你需要使用 "w "或 "a "中的一个,这取决于你是在文件中添加新行还是重写整个文件。