我在为我的名字和分数做读取文件时遇到了麻烦。
我可以阅读这个名字,但我无法阅读我的分数。
所以我将发布两个代码。 一个是完整的,第二个是读取文件代码。
我正在尝试保存并阅读保存文件以获取我的姓名和分数。
但是,我只能保存“姓名”并且分数永远保持为0。
我可以知道我在这里犯的错误吗?
读取文件代码:
FILE * scoreboard=fopen("scoreboard.txt","");
fprintf(scoreboard,"%s %i\t\n",username, score);
scoreboard = fopen("scoreboard.txt","r");
printf("\n\n*************List of Previous Players*****************\n");
if (scoreboard == NULL)
{
printf("\nNo scores found.\n");
}
while(fscanf(scoreboard,"%s %i",username, &score) == 2)
{
printf("Name: %s\t\t",username);
printf("Scores: %i\n", score);
}
fclose(scoreboard);
完整代码:
/*CREATED BY:
Name: Tan Xian Yao
Student ID: 4323440
Task: Number Crush
Date started: 31/10/2013
Date finished: 4/12/2013
*/
/*Headers Used*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
/*Structure Used*/
typedef struct
{
int m,n;
char rows,cols, x, y;
}board;
/*Function 1*/
void disp_welcome_msg()/*Display the Welcome Message for players*/
{
printf("\n~~~~~~~~~~~WELCOME~~~~~~~~~~~~~\n");
printf("\n~~~~~~~~NUMBER CRUSH~~~~~~~~~~~~~\n");
}
/*Function 2*/
void disp_ready_msg()/*Display the "Ready" message for player before starting the game*/
{
printf("\nAre you ready for the EXCITING GAME?\n\n");
system("pause");
printf("\n*****ATTENTION******");
printf("\nPress 2,4,6,8 to release out the BOX\n\n");
}
/*Function 3*/
void board_size(board *Array)/*Give players to choose the size of the board they want*/
{
printf("\n*********PLEASE READ!!********\n");
printf("\nExample of board size: (Rows: 19, Cols: 19) and (Rows: 17, Cols: 17) and so on)\n");
printf("\nMinimum suggested board size is 9x9 and maximum is 31x31(Only for ODD numbers)\n");
printf("\nEnter the Rows:");
scanf("%i",&Array->x);
printf("Enter the Cols:");
scanf("%i",&Array->y);
Array->m = Array->x;/*Part of pointed Structure*/
Array->n = Array->y;/*Part of pointed Structure*/
}
/*Function 4*/
void generate_rand_no( int *a, int*b, char arry[51][51], board Array)/*Generate random number which will be assigned into the 2-D array*/
{
char x, y;
srand((unsigned)time(NULL)); /*Generate random number*/
for(x=0; x < Array.x; x++)
{
for(y=0; y < Array.y; y++)
{
if (y%2 != 0 && x%2 != 0)/*print number in odd number*/
do
{
arry[x][y]=rand()%7+50; /*range of random number from 2 to 8*/
}while((arry[x][y] == arry[x-2][y]) && (arry[x][y] == arry[x-4][y]) || (arry[x][y] == arry[x][y-2])&& (arry[x][y] == arry[x][y-4]));
/*Check 3 adjacent number will not appear in both horizontal and vertical axis*/
else
arry[x][y]=' ';/*for even number to print a space*/
}
}
}
/*Function 5*/
void disp_box(int *a, int *b, char arry[51][51], board Array)/*Display the 2-D array out with random numbers assigned on it randomly*/
{
Array.rows == Array.m;
Array.cols == Array.n;
/*Create the move-able box aorund a number in 2D array*/
arry[Array.m][Array.n-1] = '|';
arry[Array.m][Array.n+1] = '|';
arry[Array.m-1][Array.n] = '=';
arry[Array.m+1][Array.n] = '=';
}
/*Function 6*/
void disp_arr(board Array, char arry[51][51])/*Display the 2-D array out with random numbers assigned on it randomly*/
{
/*Used for loops*/
int c,d;
printf("\n\n>>>>>>>>>>>>>>>>>>>>>>NUMBER CRUSH<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n\n");
for(c=0; c<Array.x; c++)
{
printf("\n");
for(d=0; d<Array.y; d++)
{
printf(" %c ", arry[c][d]);
}
printf("\n");
}
}
/*Function 7*/
void disp_menu(int score)/*Display menu to guide players when moving the box*/
{
printf("\n*Total Score Collected: %i\n\n", score);
printf("\n*Menu:");
printf("\n*Press 6 - Move the box RIGHT");
printf("\n*Press 4 - Move the box LEFT");
printf("\n*Press 8 - Move the box UP");
printf("\n*Press 2 - Move the box DOWN");
printf("\n*Press 5 - Swap the Numbers");
printf("\n*Press Q - Quit Game or Give Up");
printf("\nPlease enter your next action <case senstitive>:");
}
/*Function 8*/
void swap_menu()/*Allow players to know what they can do after selecting a number to swap. */
{
printf("*Menu:\n");
printf("*Press 6 to move the box to the right\n");
printf("*Press 4 to move the box to the left\n");
printf("*Press 8 to move the box up\n");
printf("*Press 2 to move the box down\n");
}
/*Function 9*/
int move_num(char *input, int *a, int *b ,int score, char arry[51][51], board Array)/*Moving the box to find similar numbers*/
{
do
{
*input=getch(); /*Movement with no "Press enter"*/
if ( *input == '8' && *a != 1 )/*Box moves UP*/
{
system("cls");
arry[*a+1][*b]=' ';
arry[*a][*b-1]=' ';
arry[*a][*b+1]=' ';
*a=*a-2;
arry[*a-1][*b]='=';
arry[*a+1][*b]='=';
arry[*a][*b-1]='|';
arry[*a][*b+1]='|';
/*Pass functions*/
//fflush (stdin);
disp_arr(Array, arry);
disp_menu(score);
}
else if ( *input == '2' && *a != Array.x - 2 )/*Box moves UP*/
{
system("cls");
arry[*a-1][*b]=' ';
arry[*a][*b-1]=' ';
arry[*a][*b+1]=' ';
*a=*a+2;
arry[*a-1][*b]='=';
arry[*a+1][*b]='=';
arry[*a][*b-1]='|';
arry[*a][*b+1]='|';
/*Pass functions*/
//fflush (stdin);
disp_arr(Array, arry);
disp_menu(score);
}
else if( *input == '4' && *b != 1 )/*Box moves LEFT*/
{
system("cls");
arry[*a-1][*b]=' ';
arry[*a+1][*b]=' ';
arry[*a][*b+1]=' ';
*b=*b-2;
arry[*a-1][*b]='=';
arry[*a+1][*b]='=';
arry[*a][*b-1]='|';
arry[*a][*b+1]='|';
/*Pass functions*/
//fflush (stdin);
disp_arr(Array, arry);
disp_menu(score);
}
else if ( *input == '6' && *b != Array.y - 2)/*Box moves RIGHT*/
{
system("cls");
arry[*a-1][*b]=' ';
arry[*a+1][*b]=' ';
arry[*a][*b-1]=' ';
*b=*b+2;
arry[*a-1][*b]='=';
arry[*a+1][*b]='=';
arry[*a][*b-1]='|';
arry[*a][*b+1]='|';
/*Pass functions*/
//fflush (stdin);
disp_arr(Array, arry);
disp_menu(score);
}
else if (*input == '5')/*Swap*/
{
system("cls");
return 0;
}
}while(*input != 'q');
system("cls");
printf("GOOD BYE AND SEE YOU AGAIN!");
getch();/*Press any key to proceed*/
exit(0);/*Exit Program*/
return 0;
}
/*Function 10*/
int swap_num(int *a, int *b,char *Swap,char arry[51][51],int score, int *select, board Array)/*Swap using pointers*/
{
/*Temporary variable to used and hold when Swapping two numbers*/
int temp = 0;
/*Swap using pointers*/
*Swap = '5';
/*Pass functions*/
disp_arr(Array, arry);
disp_menu(score);
system("cls");
disp_arr(Array, arry);
swap_menu();
do
{
*Swap = getch();
if (*Swap == '8' && *a != 1)
{
/*Clear the screen*/
system("cls");
*select = 1;
arry[*a-2][*b-1] = '|';
arry[*a-2][*b+1] = '|';
arry[*a+1][*b] = '=';
/*Pass functions*/
disp_arr(Array, arry);
/*To clear the argument in fflush() like memory*/
//fflush(stdin);
}
else if (*Swap == '2' && *b != Array.x-2)
{
system("cls");
*select = 2;
arry[*a-2][*b-1] = '|';
arry[*a-2][*b+1] = '|';
disp_arr(Array, arry);
//fflush(stdin);
}
else if (*Swap == '4' && *b != 1)
{
system("cls");
*select = 3;
arry[*a][*b-3] = ' ';
arry[*a+2][*b-1]=' ';
arry[*b-2][*b-1]=' ';
disp_arr(Array, arry);
}
else if (*Swap == '6' && *b != Array.y-2)
{
system("cls");
*select = 4;
arry[*a][*b+3] = ' ';
arry[*a+2][*b-1]=' ';
arry[*a-2][*b-1]=' ';
disp_arr(Array, arry);
//fflush(stdin);
}
if (*select == 1)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a-2][*b];
arry[*a-2][*b] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else if (*select == 2)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a+2][*b];
arry[*a+2][*b] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else if (*select == 3)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a][*b-2];
arry[*a][*b-2] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else if (*select == 4)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a][*b+2];
arry[*a][*b+2] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else
{
return 0;
}
}while (*Swap != 'q');
system("cls");
return 0;
}
/*Function 11*/
/*To verify the matching numbers*/
int verify(int *a, int *b, char c, char d, char arry[51][51],int score, int *valid, board Array)
{
int temp=0;
for ( c = 0 ;c < Array.x ; c++)
for ( d = 0 ;d < Array.y ; d++ )
{
if (c %2 != 0 && d %2 != 0)
{
if (arry[c][d] == arry[c][d+2] && arry[c][d] == arry[c][d-2])/*Clear or boom 3 same numbers horizontally*/
{
*valid = 1;
/*For every 3 same numbers matched, 3 points will be given*/
score+=3;
/*Clears the box after swapping*/
arry[*a][*b-1] =' ';
arry[*a][*b+1] =' ';
arry[*a-1][*b] =' ';
arry[*a+1][*b] =' ';
/*Make the box to show at the 3 identical match of numbers*/
arry[c][d-1] ='|';
arry[c-1][d-2] ='=';
arry[c+1][d-2] ='=';
arry[c-1][d] ='=';
arry[c+1][d] ='=';
arry[c-1][d+2] ='=';
arry[c+1][d+2] ='=';
arry[c][d+1] ='|';
arry[c][d-3] ='|';
arry[c][d+3] ='|';
system("cls");
disp_arr(Array, arry);
printf("Awesome!!!!\n");
printf("Press any key to continue crushing!...\n");
/*To produce a beep sound when matching 3 same numbers*/
printf("\a\n");
/*Press anykey to proceed to next step*/
getch();
/*Clear the 3 identical number and also the indicator*/
arry[c][d-1] =' ';
arry[c-1][d-2] =' ';
arry[c+1][d-2] =' ';
arry[d-1][d] =' ';
arry[c+1][d] =' ';
arry[c-1][d+2] =' ';
arry[c+1][d+2] =' ';
arry[c][d+1] =' ';
arry[c][d-3] =' ';
arry[c][d+3] =' ';
arry[c][d] =' ';
arry[c][d+2] =' ';
arry[c][d-2] =' ';
/*Clear the screen*/
system("cls");
/*Pass Functions*/
disp_arr(Array, arry);
for ( c = c ; c > 1; c-- )
{
/*The numbers above will replace the empty space*/
arry[c][d]=arry[c-2][d];
arry[c][d+2]=arry[c-2][d+2];
arry[c][d-2]=arry[c-2][d-2];
}
/*Generate new random number after the number drops*/
arry[1][d]=rand()%7 +50;
arry[1][d+2]=rand()%7 +50;
arry[1][d-2]=rand()%7 +50;
system("cls");
disp_arr(Array, arry);
}
else if (arry[c][d] == arry[c+2][d] && arry[c][d] == arry[c-2][d])/*Clear or boom 3 same numbers vertically*/
{
*valid = 1;
/*For every 3 same numbers matched, 3 points will be given*/
score+=3;
/*Clears the box after swapping*/
arry[*a][*b-1]=' ';
arry[*a][*b+1]=' ';
arry[*a-1][*b]=' ';
arry[*a+1][*b]=' ';
/*Make the box to show at the 3 identical match of numbers*/
arry[c][d-1] ='|';
arry[c][d+1] ='|';
arry[c-2][d-1]='|';
arry[c-2][d+1]='|';
arry[c+2][d-1]='|';
arry[c+2][d+1]='|';
arry[c+1][d] ='=';
arry[c-1][d] ='=';
arry[c+3][d] ='=';
arry[c-3][d] ='=';
system("cls");
disp_arr(Array, arry);
printf("Awesome!!!\n");
printf("Press any key to continue crushing!...\n");
/*To produce a beep sound when matching 3 same numbers*/
printf("\a\n");
/*Press anykey to proceed to next step*/
getch();
/*Clear the 3 identical number and also the indicator*/
arry[c][d-1] =' ';
arry[c][d+1] =' ';
arry[c-2][d-1]=' ';
arry[c-2][d+1]=' ';
arry[c+2][d-1]=' ';
arry[c+2][d+1]=' ';
arry[c+1][d] =' ';
arry[c-1][d] =' ';
arry[c+3][d] =' ';
arry[c-3][d] =' ';
system("cls");
disp_arr(Array, arry);
for ( c = c ; c > 1; c-- )
{
/*The numbers above will drop replace the empty space*/
arry[c+2][d]=arry[c-4][d];
arry[c-2][d]=arry[c][d];
arry[c][d]=arry[c-2][d];
}
/*Generate new random number after the number drops*/
arry[1][d]=rand()%7 +50;
arry[3][d]=rand()%7 +50;
arry[5][d]=rand()%7 +50;
system("cls");
disp_arr(Array, arry);
}
}
}
return score;/*Return to score*/
}
/*Function 12*/
/*check for invalid moves*/
int invalidmove(int *a, int *b, char arry[51][51],int *select,int *valid, board Array)
{
int temp=0;
/*Clear the screen*/
system("cls");
/*Pass functions*/
disp_arr(Array, arry);
printf("WRONG Move!! Try Again!");
getch();
/*To clear memory*/
fflush(stdin);
if (*select == 1)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a-2][*b];
arry[*a-2][*b] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else if (*select == 2)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a+2][*b];
arry[*a+2][*b] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else if (*select == 3)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a][*b-2];
arry[*a][*b-2] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else if (*select == 4)
{
temp = arry[*a][*b];
arry[*a][*b] = arry[*a][*b+2];
arry[*a][*b+2] = temp;
system("cls");
disp_arr(Array, arry);
return 0;
}
else
{
return 0;
}
}
/*Function 13*/
/*Save read-file*/
/*void save(char username[20], int *score)
{
FILE *NumberCrush_XY = fopen("NumberCrush_XY.txt","a");
fprintf(NumberCrush_XY,"%s\t\t%i\t\n",username, &score);
fclose(NumberCrush_XY);
}
/*Function 14*/
/*Read save file after restart the program*/
/*void read_save()
{
FILE *NumberCrush_XY = fopen("NumberCrush_XY.txt","r");
char username[20];
int score = 0;
system("cls");
if (NumberCrush_XY == NULL)
{
printf("NO RECORDS ARE FOUND!");
}
printf("\t\t LIST OF PREVIOUS PLAYER\n\n");
while(fscanf(NumberCrush_XY, "%s%i",username, &score) == 2)
{
printf("\tPlayer Name:%s\t Score:%i\n",username, score);
}
printf("Bye, see you again next time!! Press any key to Exit!");
getch();
}
*/
/*Main*/
int main()
{
/*Variables*/
char arry[51][51];
int m;
int stopMove=0;
int n;
int c,d;
int score=0;
int select = 0;
int valid=0;
char input;
char username[100];
char id[100];
board Array;
/*Color for background*/
system("COLOR 80");
/*Pass Functions*/
disp_welcome_msg();
printf("\nPLEASE ENTER YOUR USERNAME BELOW\n");
printf("\nUSERNAME:");
scanf("%s", &username);
printf("\nGood day, %s. Hope you can CRUSH the numbers easily!!!...");
FILE * scoreboard=fopen("scoreboard.txt","");
fprintf(scoreboard,"%s %i\t\n",username, score);
scoreboard = fopen("scoreboard.txt","r");
printf("\n\n*************List of Previous Players*****************\n");
if (scoreboard == NULL)
{
printf("\nNo scores found.\n");
}
while(fscanf(scoreboard,"%s %i",username, &score) == 2)
{
printf("Name: %s\t\t",username);
printf("Scores: %i\n", score);
}
fclose(scoreboard);
printf("\n");
/*Pause system and press any ket to proceed*/
system("pause");
/*clear the screen*/
system("cls");
system("COLOR 08");
board_size(&Array);
/*Pointed structure in choosing board size*/
m = Array.x;
n = Array.y;
system("cls");
disp_ready_msg();
system("pause");
system("cls");
system("COLOR F0");
/*Pass functions*/
generate_rand_no(&m, &n, arry, Array);
disp_box(&m, &n, arry, Array);
disp_arr(Array, arry);
disp_menu(score);
/*Loop the pass functions and enable the box to move around the board with no limits*/
do
{
move_num(&input, &m, &n ,score, arry, Array);
swap_num(&m, &n, &input, arry, score, &select, Array);
score=verify(&m, &n, c, d, arry, score, &valid, Array);
valid = 0;
if (valid == 0)
{
invalidmove(&m,&n,arry,&select,&valid, Array);
}
}while (input != 'Q' || input != 'q');
system("cls");
/*Pass functions for save and read file*/
//save(username, score);
//read_save();
}
答案 0 :(得分:0)
您是第二次打开文件,而不是先关闭它。由于标准I / O被缓冲,因此读取时文件可能显示为空。你应该做什么:
scoreboard = fopen ("scoreboard.txt", "w");
if (scoreboard != NULL) {
do the writing
fclose (scoreboard);
} else {
scream and die
}
scoreboard = fopen ("scoreboard.txt", "r");
if (scoreboard != NULL) {
do the reading
fclose (scoreboard);
} else {
scream and die
}
答案 1 :(得分:0)
我可以知道我在这里犯的错误吗?
save
函数的调用。save(username, score);
和声明void save(char username[20], int *score)
不兼容。fprintf(NumberCrush_XY,"%s\t\t%i\t\n",username, &score);
&score
错误,*score
是正确的,而声明void save(char username[20], int score)
,fprintf(..., score);
会。