简体   繁体   English

C:永远不会得到返回语句

[英]C: never getting to return statement

I am implementing a program which hides messages in ppm files and then retrieves them.我正在实施一个程序,该程序将消息隐藏在 ppm 文件中,然后检索它们。 One of the last things I need to do is to "return 2" if there is a problem opening the file.如果打开文件出现问题,我需要做的最后一件事是“返回 2”。 If I enter a file which can't be opened (file doesn't exist or wrong extension, etc) then it will display the error message on stderr, but for some reason it won't execute the very next line, "return 2"!如果我输入一个无法打开的文件(文件不存在或错误的扩展名等),那么它将在 stderr 上显示错误消息,但由于某种原因它不会执行下一行,“返回 2 “!

It simply then returns 0 instead of returning 2.然后它只是返回 0 而不是返回 2。

int load_ppm_image(const char *input_file_name, unsigned char **data, int *n,
               int *width, int *height, int *max_color)
{
    char line[80];
    char c;

    FILE * image_file;
    //image_file = fopen(input_file_name, "rb");

    if (fopen(input_file_name, "rb") == NULL)  // checks to see if file cant be opened
    {
        fprintf(stderr, "The input image file could not be opened\n");
        return 2;  // why isn't this being returned???
    }
    else
    {
        image_file = fopen(input_file_name, "rb");
    }

    fscanf(image_file, "%s", line);

    fscanf(image_file, "%d %d", width, height);

    *n = (3 * (*width) * (*height));

    fscanf(image_file, "%d%c", max_color, &c);
    *data = (unsigned char *)malloc((*n)*sizeof(unsigned char));
    size_t m = fread(*data, sizeof(unsigned char), *n, image_file);

    assert(m == *n);
    fclose(image_file);
    return 0;
}

int hide_message(const char *input_file_name, const char *message, const char *output_file_name)
{
    unsigned char * data;
    int n;
    int width;
    int height;
    int max_color;

    n = 3 * width * height;
    int code = load_ppm_image(input_file_name, &data, &n, &width, &height, &max_color);

    if (code)
    {
        // return the appropriate error message if the image doesn't load correctly
        return code;
    }

    int len_message;
    int count = 0;
    unsigned char letter;

    // get the length of the message to be hidden
    len_message = (int)strlen(message);

    for(int j = 0; j < len_message; j++)
    {

        letter = message[j];
        int mask = 0x80;

        // loop through each byte
        for(int k = 0; k < 8; k++)
        {
            if((letter & mask) == 0)
            {
                //set right most bit to 0
                data[count] = 0xfe & data[count];
            }
            else
            {
                //set right most bit to 1
                data[count] = 0x01 | data[count];
            }
            // shift the mask
            mask = mask>>1 ;
            count++;
        }
    }
    // create the null character at the end of the message (00000000)
    for(int b = 0; b < 8; b++){
        data[count] = 0xfe & data[count];
        count++;
    }

    // write a new image file with the message hidden in it
    int code2 = write_ppm_image(output_file_name, data, n, width, height, max_color);

    if (code2)
    {
        // return the appropriate error message if the image doesn't load correctly
        return code2;
    }

    return 0;
}

Edit:编辑:

int main(int argc, const char * argv[])
{
    if (argc < 2 || argv[1][0] != '-')
    {
        // the user didn't enter a switch
        fprintf(stderr, "Usage: no switch was entered.\n");
        return 1;
    }

    else if ((strcmp(argv[1], "-e") == 0) && (argc == 5))
    {
        // hides a message in an output file
        hide_message(argv[2], argv[3], argv[4]);
    }

    else if ((strcmp(argv[1], "-d") == 0) && (argc == 3))
    {
        // retrieves a hidden message in a given file
        retrieve_message(argv[2]);
    }

    else
    {
        // all other errors prompt a general usage error
        fprintf(stderr, "Usage: error");
        return 1;
    }
}

Program ended with exit code: 0

The exit code of a C program is the return value of main or the status value in an explicit exit function call. C 程序的退出代码是main的返回值或显式exit函数调用中的status值。 In your case, the load_ppm_image returns a value to its caller function hide_message which in turn returns the same value to its caller function main .在你的情况下, load_ppm_image的值返回到调用函数hide_message这反过来又返回相同的值到它的调用函数main However, main does not explicitly return anything for the case that calls hide_message .但是, main不会为调用hide_message的情况显式返回任何内容。 The C standard specifies that main implicitly returns 0 if it reaches the end of the function without an explicit return. C 标准规定,如果main在没有显式返回的情况下到达函数末尾,则它隐式返回 0。 Hence the exit code of 0 in your case.因此,在您的情况下,退出代码为 0。

To get the behaviour you desire modify your main code to return the hide_message return value.要获得您想要的行为,请修改您的main代码以返回hide_message返回值。

else if ((strcmp(argv[1], "-e") == 0) && (argc == 5))
{
    // hides a message in an output file
    return (hide_message(argv[2], argv[3], argv[4]));
}

From code :从代码:

In main function you are calling the hide_message function, but not collecting return value of hide_message()在主函数中,您正在调用 hide_message 函数,但不收集 hide_message() 的返回值

hide_message(argv[2], argv[3], argv[4]);

So the main function is ending with return 0.所以 main 函数以 return 0 结束。

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

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