C语言中如何将数据保存到.txt文件中

C语言中如何将数据保存到.txt文件中

我是一个初学者,我在学校的一个项目上遇到了麻烦。我已经有了程序的大纲,运行得很好。我遇到的问题是如何将新数据保存到players.txt文件中。

如果我和一个玩家玩游戏,他们赢了10分,我想把它保存到.txt文件中,并在我查看前5名的余额时显示出来,或者当我关闭程序并打开它再次与该玩家玩游戏时显示出来。

目前,它只在我停留在play()函数中时保存赢钱/输钱数据。然后,它恢复为players.txt文件中的原始数字。

代码语言:javascript复制#include

#include

#include

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();

}

相关推荐

给你花审核要多久?
best365官网手机版

给你花审核要多久?

📅 08-12 👁️ 4100
母的五笔怎么打?
bet体育365官网怎么样

母的五笔怎么打?

📅 10-21 👁️ 6788
淘宝店铺解绑流程详解:如何解除与支付宝的绑定?
bet体育365官网怎么样

淘宝店铺解绑流程详解:如何解除与支付宝的绑定?

📅 07-30 👁️ 7699
日韩联手邀中国, 共办2046世界杯, 答应还是不答应?
best365官网手机版

日韩联手邀中国, 共办2046世界杯, 答应还是不答应?

📅 07-29 👁️ 5804
常见问题
bet体育365官网怎么样

常见问题

📅 10-18 👁️ 2409
网页转PDF
bet体育365官网怎么样

网页转PDF

📅 09-14 👁️ 8645