gpt4 book ai didi

c - 为什么我的文件内容消失以及如何正确读取该文件?

转载 作者:行者123 更新时间:2023-11-30 16:49:30 24 4
gpt4 key购买 nike

我有一个 connect 4 程序,允许用户输入行数/列数以及获胜所需的连接长度。该程序的工作原理是,在游戏过程中的任何时候,您都可以键入“save”而不是您想要放置游戏 block 的列,并且游戏设置(num_rows、num_columns、length_to_win)将被写入名为 settings 的文件中.txt

我可以确认这有效,并且 settings.txt 保存的数据如下所示:

7 7 4

其中每个数字都是一个设置,后面有一个空格。

现在,我尝试读取这些整数并将它们设置为等于相应的变量,作为我的(希望成功的)加载游戏方法的开始;但是,当我尝试按照下面代码中所示的方式使用 fscanf 时,没有任何反应...并且当我之后查看 settings.txt 时,它是空白的..这是为什么?

主要方法

int main (int argc, char *argv[]) { 

setvbuf(stdout, NULL, _IONBF, 0);
printf("Starting Game\n");


// the default values for board size if no command line arguments are used
int index;
int num_rows = 7;
int num_columns = 7;
int length_to_win = 4;
FILE *fp = fopen("settings.txt", "r");


// this loop checks for command line arguments and sets game variables accordingly.
for(index = 0; index < argc; ++index) {

if ( strncmp( argv[index], "-l", 5) == 0 ) {
fscanf(fp, "%d %d %d", &num_rows, &num_columns, &length_to_win);
}
if ( strncmp( argv[index], "-h", 5) == 0 ) {
num_rows =atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-height", 5) == 0 ) {
num_rows =atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-w", 5) == 0 ) {
num_columns = atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-width", 5) == 0 ) {
num_columns = atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-s", 5) == 0 ) {
num_rows = atoi(argv[index + 1]);
num_columns = atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-square", 5) == 0 ) {
num_rows = atoi(argv[index + 1]);
num_columns = atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-c", 5) == 0 ) {
length_to_win = atoi(argv[index + 1]);
}
if ( strncmp( argv[index], "-connect", 5) == 0 ) {
length_to_win = atoi(argv[index + 1]);
}
}



// these conditionals check for valid board size
if (num_rows <= 0 || num_columns <= 0 ){
printf("%s\n","You entered a width or length that was invalid." );
}
if (length_to_win <= 0 || length_to_win > (num_rows - 1)) {
printf("%s\n","You entered a winning length that was invalid." );
}


// this creates the board
int array[num_rows][num_columns];
initialize(num_rows, num_columns, array);
int answer;
int player = 0;

printf("%s\n", "*********************");
printf("%s\n", " Starting Board ");
printf("%s\n", "*********************");
puts("\n");
printBoard(num_rows, num_columns, array);
puts("\n");
printf("Player: %ds Turn\n", player + 1);


/*Start game loop*/
while(1) {

// prompts the user to select which column they want their piece to be placed
// -1 on the temp because the first column is technically 0 so if a player
// wants to place their piece in column "1", it'll be placed at index[0] accordingly
printf("%s\n", "Enter Column # To Place Token");
int column;
char temp[20];
scanf("%s", temp);

if (strncmp (temp, "save", 5) == 0){

// this writes the game settings to a file
int *rows = &num_rows;
int *cols = &num_columns;
int *len = &length_to_win;
FILE *fp = fopen("settings.txt", "w+");
fprintf(fp, "%d ", *rows);
fprintf(fp, "%d ", *cols);
fprintf(fp, "%d ", *len);
printf("Game Saved\n");
fclose(fp);

}
else {
column = atoi(temp) - 1;
}

if ((column < 0 || column > (num_columns - 1)) && (strncmp (temp, "save", 5) != 0) ) {
printf("%s\n","You entered a column that was invalid. Please try again." );
continue;
}

int attmpt = place_token(player, column, num_rows, num_columns, array);
if (attmpt != 1 && (strncmp (temp, "save", 5) != 0)) {
printf("%s\n","This row is already full. Please try again." );
continue;
}

printf("%s\n", "************************");
printf("%s\n", " Board Updated ");
printf("%s\n", "************************");
puts("\n");
printBoard(num_rows, num_columns, array);
puts("\n");




if (checkFullBoard(num_rows, num_columns, array)) {
printf("%s\n","This game is a tie. Thanks for Playing.\n");
return 0;
}

// this if-statement will constantly be run while the game progresses,
// meaning that winner will be called at every turn and
// all of the win conditions will be checked until a winner is found
int isWin = winner(num_rows, num_columns, length_to_win, array);
if(isWin != -1) {
char temp2[20];
printf("Player: %d is the winner! Thanks for Playing.\n", isWin + 1);
printf("Play again? (enter 'y' to continue)\n");
scanf("%s", temp2);

if (strncmp (temp2, "y", 5) == 0){
initialize(num_rows, num_columns, array);
printBoard(num_rows, num_columns, array);
}
else {
printf("Game over, goodbye!\n");
return 0;
}
}

// if a winner is not found then this if/else will continue to switch
// between players at the end of each turn
if ((strncmp (temp, "save", 5) != 0)) {
if (player == 1) {
player = 0;
}
else {
player = 1;
}
}

printf("Player: %ds Turn\n", player +1);
} // end of while loop

return 0;
} // end of main

最佳答案

根据silentboy的建议,解决方案是将“w+”替换为“a”。

关于c - 为什么我的文件内容消失以及如何正确读取该文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42571680/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com