简体   繁体   English

函数/结构/指针中的分段错误

[英]Segmentation Error in Function/Struct/Pointer

I am trying to read in two images and then output them into two multidimensional arrays and then later in the program flip/blend/output the images. 我试图读取两个图像,然后将它们输出到两个多维数组,然后在程序中翻转/混合/输出图像。 Does anyone have any ideas why I continue to get a segmentation error? 有没有人有任何想法,为什么我继续得到分段错误? I know it's within the ReadImages call beacuse it complies and then when I execute it -- the first call comes up, but fails after that.... 我知道它在ReadImages调用中,因为它符合它,然后当我执行它时 - 第一个调用出现,但在此之后失败....

void ReadImages(struct ImageType *imgur, struct ImageType *imgur2)
{
    int i = 0, j = 0;
    char filename[30];
    char filename2[30];

    //Scanning in the first file.
    FILE *inputfile;
    fprintf(stdout, "Please enter the filename/location of the first image\n");
    fscanf(stdin, "%c", &filename);
    inputfile = fopen(filename, "r");
    if(inputfile = NULL)
    {
        fprintf(stderr,"Sorry you didn't specify correctly\n");
    }

    fscanf(inputfile,"%[^\n]%c", imgur->ppImage, &imgur->newlinechar);
    fscanf(inputfile,"%[^\n]%c", imgur->comment, &imgur->newlinechar);
    fscanf(inputfile,"%i %i", &imgur->width, &imgur->height);
    fscanf(inputfile,"%i", &imgur->maxColor);

    for(i = imgur->height - 1; i >= 0; i--)
    {
        for(j = 0; j > imgur->width; j++)
        {
            fscanf(inputfile,"%i", &imgur->image[i][j].red);
            fscanf(inputfile,"%i", &imgur->image[i][j].green);
            fscanf(inputfile,"%i", &imgur->image[i][j].blue);
        }
    }

    //Scanning in the second file.
    FILE *inputfile2;
    fprintf(stdout, "Please enter the filename/location of the second image\n");
    fscanf(stdin, "%c", filename2);
    inputfile2 = fopen(filename2, "r");
    if(inputfile2 = NULL)
    {
        fprintf(stdout,"Sorry you didn't specify the filename/location correctly\n");
    }

    fscanf(inputfile2, "%[^\n]%c", imgur2->ppImage, &imgur2->newlinechar);
    fscanf(inputfile2, "%[^\n]%c", imgur2->comment, &imgur2->newlinechar);
    fscanf(inputfile2, "%i %i", &imgur2->width, &imgur2->height);
    fscanf(inputfile2, "%i", &imgur2->maxColor);

    for(i = imgur2->height - 1; i >= 0; i--)
    {
        for(j = 0; j > imgur->width; j++)
        {
            fscanf(inputfile2,"%i", &imgur2->image[i][j].red);
            fscanf(inputfile2,"%i", &imgur2->image[i][j].green);
            fscanf(inputfile2,"%i", &imgur2->image[i][j].blue);
        }
    }
}

You have 你有

fscanf(stdin, "%c", &filename);

to read in the filename, but %c is the format specifier for a single character. 读取文件名,但%c是单个字符的格式说明符。 You want %s , for a string, instead. 您希望%s代替字符串。 The & in front of filename is also unnecessary. filename前面的&也是不必要的。

The same thing applies for: 同样的事情适用于:

fscanf(stdin, "%c", filename2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM