无法从可执行文件创建 ppm 文件 (C)

发布于 2025-01-17 10:08:27 字数 1466 浏览 0 评论 0原文

我试图从 C 程序内部创建 ppm 文件,但不知何故它不起作用。

当我在 IDE 中运行程序时,它工作得很好。程序被执行并在文件的文件夹中创建文件。

但是,一旦我构建它并双击打开可执行文件,该程序就会在终端中运行,但不会创建文件。

我正在Mac上工作,这是相关的代码,提前感谢大家!

// from generalSettings.h

#define WIDTH 1100
#define HEIGHT 966
#define MYFILENAME "testimage.ppm"
int pictureArray[HEIGHT][WIDTH];


//from the main.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "generalSettings.h"

int main()
{
    triangle firstParentTriangle;

    // these two functions "draw" sierpinsky triangles in the pictureArray
    drawFirstParentTriangle(&firstParentTriangle);
    drawChildTriangles(firstParentTriangle, numberOfRecursions);


    create_ppm();
    return 0;
}


void create_ppm()
{
    unsigned char color_black[] = "000 000 000\n";
    unsigned char color_white[] = "255 255 255\n";

    FILE *p_file = fopen(MYFILENAME, "w");
    if (NULL != p_file)
    {
        fprintf(p_file, "P3\n %d %d\n 255\n", WIDTH, HEIGHT);
        for (int i = 0; i < HEIGHT; i++)
            for (int j = 0; j < WIDTH; j++)
                if (1 == pictureArray[i][j])
                    fprintf(p_file, color_black);
                else
                    fprintf(p_file, color_white);

        fclose(p_file);
    }
}

在这里,我用本地变量替换了前景和背景变量。

一般来说,程序会在某些元素上将 1 插入到 pictureArray[][] 中,而将其他元素保留为 0。

对于我的问题,这应该是可重现示例的相关代码。

编辑:问题解决者。由于缺少路径,文件是在用户文件夹中创建的。

I am trying to create a ppm file from inside a C program, but somehow it doesn't work.

It works fine when I am inside the IDE and run the program there. The program is executed and the file created inside the file's folder.

But once I build it and open the executable with a double click, the program runs in the terminal but does not create the file.

I am working on a mac and this is the relevant code, thanks in advance you all!

// from generalSettings.h

#define WIDTH 1100
#define HEIGHT 966
#define MYFILENAME "testimage.ppm"
int pictureArray[HEIGHT][WIDTH];


//from the main.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "generalSettings.h"

int main()
{
    triangle firstParentTriangle;

    // these two functions "draw" sierpinsky triangles in the pictureArray
    drawFirstParentTriangle(&firstParentTriangle);
    drawChildTriangles(firstParentTriangle, numberOfRecursions);


    create_ppm();
    return 0;
}


void create_ppm()
{
    unsigned char color_black[] = "000 000 000\n";
    unsigned char color_white[] = "255 255 255\n";

    FILE *p_file = fopen(MYFILENAME, "w");
    if (NULL != p_file)
    {
        fprintf(p_file, "P3\n %d %d\n 255\n", WIDTH, HEIGHT);
        for (int i = 0; i < HEIGHT; i++)
            for (int j = 0; j < WIDTH; j++)
                if (1 == pictureArray[i][j])
                    fprintf(p_file, color_black);
                else
                    fprintf(p_file, color_white);

        fclose(p_file);
    }
}

Here, I have replaced the foreground and background variables with local ones.

Generally, the program inserts 1 to the pictureArray[][] on certain elements and leaves others with a 0.

For my problem, this should be the relevant code for a reproducable example.

EDIT: Problem solver. File was created in the user folder due to missing path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文