标题:C语言二维数组在文件中读写的问题,谢谢
只看楼主
new123123
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-4-10
结帖率:0
已结贴  问题点数:10 回复次数:4 
C语言二维数组在文件中读写的问题,谢谢
这是一个用二位数组写的五子棋小游戏的代码,我的思路是通过键盘输入坐标显示棋子,当输0 0时保存棋盘并结束游戏,下一次进入程序时,选择1.继续游戏;2.显示上局棋盘。但在文件保存方便遇到了问题。文件的读写我再代码中已经注释出来了,希望各位前辈能够解疑答惑,不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void printChessboard();
void playChess();
void Load();
int judge(int,int);
#define N    15
int chessboard[N + 1][N + 1]={0};
int i,j;


int whoseTurn = 0;
//读取棋盘
void Load()/*读取棋局*/

{
    FILE* fp;

    if( ( fp = fopen ("date.txt", "r" ) ) == NULL )
    {
        exit(0);
    }
    else

        for (i = 0; i <= N+1; i++)
        {
            for (j = 0; j <= N+1; j++)
            {
                fscanf(fp,"%d",&chessboard[i][j]);
            }
        }
        for (i = 0; i <= N+1; i++)
        {
            for (j = 0; j <= N+1; j++)
            {
                if (0 == i)
                printf("%3d",j);
                else if (j == 0)
                printf("%3d",i);
                else if (1 == chessboard[i][j])
                printf("  X");
                else if (2 == chessboard[i][j])
                printf("  O");
                else if(0 == chessboard[i][j])
                printf("  *");
                else
                    exit(0);
        }
        printf("\n");
    }

}
void printChessboard()
{
    int i, j;
    for (i = 0; i <= N; i++)
    {
        for (j = 0; j <= N; j++)
        {
            if (0 == i)
                printf("%3d",j);
            else if (j == 0)
                printf("%3d",i);
            else if (1 == chessboard[i][j])
                printf("  X");
            else if (2 == chessboard[i][j])
                printf("  O");
            else if(0 == chessboard[i][j])
                printf("  *");
            else
                    exit(0);
        }
        printf("\n");
    }
}
void playChess(void)
{
    FILE *fp;
   
    fp=fopen("date.txt","w");
    printChessboard();

    if (1 == whoseTurn % 2)
    {
        printf("轮到玩家1,请输入棋子的位置,格式为行号+空格+列号+1:");
        scanf("%d%d", &i, &j);
        for (int a = i; a<=i;a++)
        {
            for (int b= j; b <= j;b++)
            {
                scanf("%d",&chessboard[a][b]);
                fprintf(fp,"%d",chessboard[i][j]);//保存棋盘
            }
        }
        fclose(fp);
        if(i!=0)
            chessboard[i][j] = 1;
        else
            exit(0);
    }   
    else
    {
        printf("轮到玩家2,请输入棋子的位置,格式为行号+空格+列号+2:");
        scanf("%d%d", &i, &j);
        for (int a = i; a<= i;a++)
        {
            for (int b= j; b <=j;b++)
            {
                scanf("%d",&chessboard[a][b]);
                fprintf(fp,"%d",chessboard[i][j]);//保存棋盘
            }
        }
        fclose(fp);
        if(i!=0)
            chessboard[i][j] = 2;
        else
            exit(0);
    }
    system("cls");

    if (judge(i, j))
    {
        if(1 == whoseTurn % 2)
        {
            printf("玩家1胜!\n");
            exit(0);
        }
        else
        {
            printf("玩家2胜!\n");
            exit(0);
        }
    }
}

int judge(int x, int y)//判断输赢
{
    int i, j;
    int t = 2 - whoseTurn % 2;

    for (i = x - 4, j = y; i <= x; i++)
    {
        if (t == chessboard[i][j] && t == chessboard[i + 1][j] && t == chessboard[i + 2][j] && t == chessboard[i + 3][j] && t == chessboard[i + 4][j])
            return 1;
    }
    for (i = x, j = y - 4; j <= y; j++)
    {
        if (t == chessboard[i][j] && t == chessboard[i][j + 1] && t == chessboard[i][j + 2] && t == chessboard[i][j + 3] && t == chessboard[i][j + 4])
            return 1;
    }
    for (i = x - 4, j = y - 4; i <= x, j <= y; i++, j++)
    {
        if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i + 1][j + 1] && t == chessboard[i + 2][j + 2] && t == chessboard[i + 3][j + 3] && t == chessboard[i + 4][j + 4])
            return 1;
    }
    for (i = x + 4, j = y - 4; i >= 1, j <= y; i--, j++)
    {
        if (j >= 1 && j <= N - 4 && t == chessboard[i][j] && t == chessboard[i - 1][j + 1] && t == chessboard[i - 2][j + 2] && t == chessboard[i - 3][j + 3] && t == chessboard[i - 4][j + 4])
            return 1;
    }

    return 0;
}
void main()
{
    printf("1.新游戏  2.显示上局棋盘\n");
    fflush(stdin);
    char choice;
    choice=getchar();
    switch(choice)
    {
    case'1':
        printf("欢迎进入游戏");
        system("cls");
        while(1)
        {
            whoseTurn++;
            playChess();
        }
        break;
    case'2':
        Load();


    }
}
搜索更多相关主题的帖子: void int for printf if 
2018-04-27 17:59
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
得分:4 
都还没说出了什么问题。我专门百度了一下,fscanf失败的话返回的应该是-1哦,不是NULL。
2018-04-27 18:08
new123123
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2018-4-10
得分:0 
回复 2楼 yangfrancis
不好意思,没描述清楚。就是二位数组无法保存到txt里,并且无法在下一次运行时读取出来。
2018-04-27 18:21
zbjzbj
Rank: 12Rank: 12Rank: 12
来 自:郑州
等 级:贵宾
威 望:52
帖 子:620
专家分:3020
注 册:2011-4-22
得分:4 
读代码,太长,耐心不够。想给你调试一下吧,游戏操作太罗嗦,没有耐心操作下去。
不过给你提醒一下,
1.文本文件写入的时候需要指定写入位置的,除非一次性顺序写入。
2.文件打开的时候要检验打开是否成功。
3.注意文件存取的文件夹。
4.存取的文档你可以找到那个文件夹用记事本或者16进制编辑器打开看一下是不是自己设想的那样。
2018-04-27 22:53
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:4 
可能是没换行
试试:
//fprintf(fp,"%d",chessboard[i][j]);//保存棋盘
fprintf(fp,"%d\n",chessboard[i][j]);//保存棋盘
或者只测试一下文件存取问题:
#include <stdio.h>
void main(void)
{
    FILE *fp;
    fp = fopen("test.txt", "w");
    fprintf(fp, "%d\n", 123);
    fprintf(fp, "%d\n", 456);
    fclose(fp);
    int a;
    fp = fopen("test.txt", "r");
    fscanf(fp, "%d", &a);
    printf("%d\n", a);
    fscanf(fp, "%d", &a);
    printf("%d\n", a);
    fclose(fp);
}
2018-04-28 04:22



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-486448-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.084415 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved