鱼C论坛

 找回密码
 立即注册
查看: 1300|回复: 12

[已解决]fprintf在一个txt文件上打印时的问题

[复制链接]
发表于 2018-10-18 19:14:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
最后是通过函数打印出来的 那我想打印在一个txt文件上   fprintf格式的第三个地方写什么进去?

fprintf(fp,"%d","这里写什么 ")




#include<assert.h>
#include<stdio.h>

#define MaxSize 20
#define Row 6
#define Col 6

typedef struct Position
{
    int _x;
    int _y;
}Position;

typedef Position SDataType;

typedef struct Stack
{
    int top;
    SDataType _arry[MaxSize];
}Stack;

typedef struct Maze
{
    int _map[Row][Col];
}Maze;

void PassMaze(Maze* m, Position enter);
void InitMaze(Maze* m, int map[Row][Col]);
void PrintMaze(Maze* m);
int IsValidEnter(Maze* m, Position enter);//是否为入口
void StackPush(Stack* ps, SDataType data);
void StackInit(Stack* ps);
void StackPop(Stack* ps);
int IsExit(Position pos, Position enter);//是否为出口
int IsPass(Maze* m, Position pos);
int StackEmpty(Stack* ps);
SDataType StackTop(Stack* ps);//返回栈顶元素



void InitMaze(Maze* m, int map[Row][Col])
{
    assert(m != NULL);
    int i = 0;
    int j = 0;
    for (i = 0; i < Row; i++)
    {
        for (j = 0; j < Col; j++)
        {
            m->_map[i][j] = map[i][j];
        }
    }
}

void PrintMaze(Maze* m)
{
    assert(m != NULL);
    int i = 0;
    int j = 0;
    for (i = 0; i < Row; i++)
    {
        for (j = 0; j < Col; j++)
        {
            printf("%d  ", m->_map[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

int IsValidEnter(Maze* m, Position enter)
{
    assert(m != NULL);
    if ((enter._x >= 0 || enter._x < Row) && (enter._y >= 0 || enter._y < Col))
    {
        return 1;
    }
    return 0;
}

void StackPush(Stack* ps, SDataType data)
{
    assert(ps != NULL);
    if (ps->top == MaxSize)
        return;
    else
    {
        ps->_arry[ps->top] = data;
        ps->top++;
    }
}

void StackInit(Stack* ps)
{
    assert(ps != NULL);
    ps->top = 0;
}

void StackPop(Stack* ps)
{
    assert(ps != NULL);
    if (ps->top)
        ps->top--;
}

int IsExit(Position pos, Position enter)
{
    if ((pos._x == 0 || pos._y == Row - 1) || ((pos._y == 0 || pos._y == Col - 1) && (pos._x !=enter._x || pos._y !=enter._y)))
    {
        return 1;
    }
    return 0;
}

int IsPass(Maze* m, Position pos)
{
    assert(m != NULL);
    if (1 == m->_map[pos._x][pos._y])
    {
        return 1;
    }
    return 0;
}

int StackEmpty(Stack* ps)
{
    assert(ps != NULL);
    if (0 == ps->top)
        return 1;//栈空
    return 0;
}

SDataType StackTop(Stack* ps)
{
    assert(ps != NULL);
    return ps->_arry[ps->top - 1];
}

void PassMaze(Maze* m, Position enter)
{
    assert(m != NULL);
    if (!IsValidEnter(m, enter))//如果入口不合法
    {
        printf("入口不合法!!!\n");
        return;
    }
    else
    {
        Stack s;
        StackInit(&s);
        StackPush(&s, enter);
        while (!StackEmpty(&s))//当栈不空时
        {
            Position pos;
            Position next;
            pos = StackTop(&s);
            m->_map[pos._x][pos._y] = 8;//将走过的路标记为2
            if (IsExit(pos, enter))//判断是不是出口
                return;
            else
            {
                //上
                next = pos;
                next._x -= 1;
                if (IsPass(m, next))//判断是否可以走通,只有为1 时才可以走通
                {
                    StackPush(&s, next);
                    continue;
                }
                //左
                next = pos;
                next._y -= 1;
                if (IsPass(m, next))
                {
                    StackPush(&s, next);
                    continue;
                }
                //右
                next = pos;
                next._y += 1;
                if (IsPass(m, next))
                {
                    StackPush(&s, next);
                    continue;
                }
                //下
                next = pos;
                next._x += 1;
                if (IsPass(m, next))
                {
                    StackPush(&s, next);
                    continue;
                }
                m->_map[pos._x][pos._y] = 3;
                StackPop(&s);
            }
        }
    }
}

void test()
{
    Maze m;
    Position enter;
    enter._x = 5;
    enter._y = 2;
    int map[Row][Col] =
  { { 0, 1, 0, 0, 1, 0 },
    { 0, 0, 1, 0, 0, 0 },
    { 0, 0, 1, 1, 1, 0 },
    { 0, 0, 1, 1, 1, 0 },
    { 0, 1, 1, 0, 1, 1 },
    { 0, 0, 1, 0, 0, 0 } };
    InitMaze(&m, map);
    PrintMaze(&m);
    PassMaze(&m, enter);
    PrintMaze(&m);
}

int main()
{
    test();
    FILE*fp;
    fp=fopen("maze.txt","w");
    fprintf(fp,"%d",       );
    fclose(fp);
    return 0;
}
最佳答案
2018-10-19 15:22:34
qpwoeiruyt 发表于 2018-10-19 15:09
不是不是!!!    我的语言组织能力比较差不好意思。。。 我是想把代码里的迷宫打印在maze.txt 里面 这 ...

所以我说你要用循环的方式,上次的代码不是有一个 print(int arr[][M]) 的吗?用同样的方式就好~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-18 19:19:52 | 显示全部楼层
当作一般的 printf() 使用就好,只不过,前面多加了一个写入的地址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 00:32:38 | 显示全部楼层
claws0n 发表于 2018-10-18 12:19
当作一般的 printf() 使用就好,只不过,前面多加了一个写入的地址

大佬  就是这里的写入地址弄不懂  能不能帮我写出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 09:19:54 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-19 00:32
大佬  就是这里的写入地址弄不懂  能不能帮我写出来

你已经写出来了呀,就是 fp,至于后面的东西,我不知道你要写啥~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 14:01:20 | 显示全部楼层
claws0n 发表于 2018-10-19 02:19
你已经写出来了呀,就是 fp,至于后面的东西,我不知道你要写啥~

fp这个是要将内容输出到fp去  但是从哪里输入我不懂

通常情况下是这样的
a=123;
FILE*fp;
    fp=fopen("maze.txt","w");
    fprintf(fp,"%d",a );
    fclose(fp);

但我这个是函数void那我要怎么引进函数的内容
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 14:09:03 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-19 14:01
fp这个是要将内容输出到fp去  但是从哪里输入我不懂

通常情况下是这样的

fprintf(fp,"%d",a );  //把 a 的值以整数的方式打印在文件 fp 上
fclose(fp); //关闭文件(同时保存)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 14:15:04 | 显示全部楼层
claws0n 发表于 2018-10-19 07:09
fprintf(fp,"%d",a );  //把 a 的值以整数的方式打印在文件 fp 上
fclose(fp); //关闭文件(同时保存)

是 我是知道这样做 但我的函数里弄不出来啊  你把我的全部代码复制然后运行一下就能看到打印我不来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 14:21:58 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-19 14:15
是 我是知道这样做 但我的函数里弄不出来啊  你把我的全部代码复制然后运行一下就能看到打印我不来

可以呀,输入在当前路径的 maze.txt
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 14:36:06 | 显示全部楼层
claws0n 发表于 2018-10-19 07:21
可以呀,输入在当前路径的 maze.txt

可以?能在maze.txt 打印出那个迷宫? 大佬能把代码放上来吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 14:39:04 | 显示全部楼层
qpwoeiruyt 发表于 2018-10-19 14:36
可以?能在maze.txt 打印出那个迷宫? 大佬能把代码放上来吗?


a=123;
FILE*fp;
    fp=fopen("maze.txt","w");
    fprintf(fp,"%d",a );   //你打印的是变量 a
    fclose(fp);

如果你要打印迷宫用循环的方式,一字一句的~~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 14:42:47 | 显示全部楼层
不是不是!!!    我的语言组织能力比较差不好意思。。。 我是想把代码里的迷宫打印在maze.txt 里面 这个a=123 是我随便举的一个例子
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-10-19 15:09:21 | 显示全部楼层
claws0n 发表于 2018-10-19 07:39
a=123;
FILE*fp;
    fp=fopen("maze.txt","w");

不是不是!!!    我的语言组织能力比较差不好意思。。。 我是想把代码里的迷宫打印在maze.txt 里面 这个a=123 是我随便举的一个例子
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-19 15:22:34 | 显示全部楼层    本楼为最佳答案   
qpwoeiruyt 发表于 2018-10-19 15:09
不是不是!!!    我的语言组织能力比较差不好意思。。。 我是想把代码里的迷宫打印在maze.txt 里面 这 ...

所以我说你要用循环的方式,上次的代码不是有一个 print(int arr[][M]) 的吗?用同样的方式就好~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 23:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表