Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中将P2.pgm图像读入2D数组_C_Arrays - Fatal编程技术网

在C语言中将P2.pgm图像读入2D数组

在C语言中将P2.pgm图像读入2D数组,c,arrays,C,Arrays,我已经开始学习图像处理,并尝试将.pgm文件读入C语言中的2D数组。我正在测试输入和输出,但程序无法正常工作,因此,我尝试进行一些更改。如何改进/修改代码以使其正常工作 #include<stdio.h> #include<stdlib.h> struct PGMstructure { int maxVal; int width; int height; int data[800][800]; }; int main() {

我已经开始学习图像处理,并尝试将.pgm文件读入C语言中的2D数组。我正在测试输入和输出,但程序无法正常工作,因此,我尝试进行一些更改。如何改进/修改代码以使其正常工作

#include<stdio.h>
#include<stdlib.h>

struct PGMstructure 
{
    int maxVal;
    int width;
    int height;
    int data[800][800];
};


int main()
{
    FILE *imagein,*imageout;

    int row, col;

    int i,j;
    int ch_int;
    struct PGMstructure *imginfo;   
    char infpath[500],outfpath[500];

    printf("Enter PGM file path:");
    scanf("%s",infpath);
    imagein = fopen(infpath,"r+");

    if(imagein == NULL)
    {
        printf("Error opening first file");
        exit(8);
    }

    while(getc(imagein) != '\n');             

    while (getc(imagein) == '#')              
    {
        while (getc(imagein) != '\n');          
    }

    fseek(imagein, -1, SEEK_CUR);
    fscanf(imagein,"%d", &imginfo->width);
    fscanf(imagein,"%d", &imginfo->height);
    fscanf(imagein,"%d", &imginfo->maxVal);
    printf("\n width  = %d\n",imginfo->width);
    printf("\n height = %d\n",imginfo->height);
    printf("\n maxVal = %d\n",imginfo->maxVal);

    for (row=0; row<imginfo->height; row++){

        for (col=0; col < imginfo->width; col++)
        {    
            fscanf(imagein,"%d", &ch_int);
            imginfo->data[row][col] = ch_int;
        }
    }

    printf("Enter path of output file:");

    scanf("%s",outfpath);
    imageout = fopen(outfpath,"w+");


    for ( i = 0 ; i < row ; i++ )
    {
        for ( j = 0 ; j < col ; j++ )
        {
            fprintf( imageout,"%d" , imginfo->data[row][col] );
        }
        printf( "\n" ) ;
    }

    return 0;
}
#包括
#包括
结构PGM结构
{
int-maxVal;
整数宽度;
内部高度;
国际数据[800][800];
};
int main()
{
文件*imagein,*imageout;
int row,col;
int i,j;
int CHU int;
结构pgm结构*imginfo;
字符输入路径[500],输出路径[500];
printf(“输入PGM文件路径:”);
scanf(“%s”,infpath);
imagein=fopen(infpath,“r+”);
if(imagein==NULL)
{
printf(“打开第一个文件时出错”);
出口(8);
}
while(getc(imagein)!='\n');
while(getc(imagein)='#')
{
while(getc(imagein)!='\n');
}
fseek(imagein,-1,SEEK_CUR);
fscanf(imagein、%d、&imginfo->width);
fscanf(图像输入、%d、&imginfo->高度);
fscanf(imagein、%d、&imginfo->maxVal);
printf(“\n宽度=%d\n”,imginfo->width);
printf(“\n高度=%d\n”,imginfo->height);
printf(“\n maxVal=%d\n”,imginfo->maxVal);
对于(行=0;行高;行++){
对于(col=0;colwidth;col++)
{    
fscanf(imagein、%d、&ch_int);
imginfo->data[row][col]=Chu int;
}
}
printf(“输入输出文件的路径:”);
scanf(“%s”,输出路径);
imageout=fopen(输出路径,“w+”);
对于(i=0;idata[row][col]);
}
printf(“\n”);
}
返回0;
}

您的代码似乎没有问题,唯一的问题是您只创建了指针,而没有将其引用到内存中的实际结构

您可以通过添加以下代码行来实现这一点,并且一切都应该按预期工作

imginfo = malloc(sizeof(struct PGMstructure));

代码的主要问题是MiteshNinja强调的问题,即忘记为结构分配空间
当您不再需要该结构时,请记住使用函数
free
释放分配给
malloc
的内存。为了避免内存泄漏,这一点非常重要

我认为值得指出的另一件事是,无论何时处理完一个文件,都应该记住使用
fclose
关闭它

然后,您可以在下面的代码中找到一些琐碎的问题

最后,也许也能帮上忙

#include<stdio.h>
#include<stdlib.h>

struct PGMstructure
{
    int maxVal;
    int width;
    int height;
    int data[800][800];
};


int main()
{
    FILE *imagein,*imageout;

    int row, col;

    int i,j;
    int ch_int;
//--- CHANGED ------ Start
    struct PGMstructure *imginfo = malloc(sizeof(struct PGMstructure));
//--- CHANGED ------ End
    char infpath[500],outfpath[500];

    printf("Enter PGM file path:");
    scanf("%s",infpath);
    imagein = fopen(infpath,"r+");

    if(imagein == NULL)
    {
        printf("Error opening first file");
        exit(8);
    }

//--- CHANGED ------ Start
    while(getc(imagein) != '\n');           // Ignore the first line in the input file

    if (getc(imagein) == '#' )              // If it is the case, ignore the second line in the input file
        {
        while(getc(imagein) != '\n');
        }
    else
        {
        fseek(imagein, -1, SEEK_CUR);
        }
//--- CHANGED ------ End

    fscanf(imagein,"%d", &imginfo->width);
    fscanf(imagein,"%d", &imginfo->height);
    fscanf(imagein,"%d", &imginfo->maxVal);
    printf("\n width  = %d\n",imginfo->width);
    printf("\n height = %d\n",imginfo->height);
    printf("\n maxVal = %d\n",imginfo->maxVal);

    for (row=0; row<imginfo->height; row++){

        for (col=0; col < imginfo->width; col++)
        {
            fscanf(imagein,"%d", &ch_int);
            imginfo->data[row][col] = ch_int;
        }
    }

//--- CHANGED ------ Start
    fclose(imagein);
//--- CHANGED ------ End

    printf("Enter path of output file:");

    scanf("%s",outfpath);
    imageout = fopen(outfpath,"w+");

//--- CHANGED ------ Start
    for ( i = 0 ; i < imginfo->height ; i++ )
    {
        for ( j = 0 ; j < imginfo->width ; j++ )
        {
            fprintf( imageout,"%d  " , imginfo->data[i][j] );
        }
            fprintf( imageout,"\n" );
    }

    fclose(imageout);
    free(imginfo);
//--- CHANGED ------ End

    return 0;
}
#包括
#包括
结构PGM结构
{
int-maxVal;
整数宽度;
内部高度;
国际数据[800][800];
};
int main()
{
文件*imagein,*imageout;
int row,col;
int i,j;
int CHU int;
//---改变------开始
struct PGMstructure*imginfo=malloc(sizeof(struct PGMstructure));
//---改变------结束
字符输入路径[500],输出路径[500];
printf(“输入PGM文件路径:”);
scanf(“%s”,infpath);
imagein=fopen(infpath,“r+”);
if(imagein==NULL)
{
printf(“打开第一个文件时出错”);
出口(8);
}
//---改变------开始
while(getc(imagein)!='\n');//忽略输入文件中的第一行
if(getc(imagein)='#')//如果是这种情况,请忽略输入文件中的第二行
{
while(getc(imagein)!='\n');
}
其他的
{
fseek(imagein,-1,SEEK_CUR);
}
//---改变------结束
fscanf(imagein、%d、&imginfo->width);
fscanf(图像输入、%d、&imginfo->高度);
fscanf(imagein、%d、&imginfo->maxVal);
printf(“\n宽度=%d\n”,imginfo->width);
printf(“\n高度=%d\n”,imginfo->height);
printf(“\n maxVal=%d\n”,imginfo->maxVal);
对于(行=0;行高;行++){
对于(col=0;colwidth;col++)
{
fscanf(imagein、%d、&ch_int);
imginfo->data[row][col]=Chu int;
}
}
//---改变------开始
fclose(imagein);
//---改变------结束
printf(“输入输出文件的路径:”);
scanf(“%s”,输出路径);
imageout=fopen(输出路径,“w+”);
//---改变------开始
对于(i=0;iheight;i++)
{
对于(j=0;jwidth;j++)
{
fprintf(imageout,“%d”,imginfo->data[i][j]);
}
fprintf(imageout,“\n”);
}
fclose(imageout);
免费(imginfo);
//---改变------结束
返回0;
}

欢迎来到StackOverflow。请具体说明问题所在。你在用你的程序做什么,你期望什么,出了什么问题?它给出了一个运行时错误,但是这里的答案很有帮助。我希望将图像内容写入另一个文件。