gpt4 book ai didi

C:永远不会返回语句

转载 作者:行者123 更新时间:2023-11-30 20:23:33 27 4
gpt4 key购买 nike

我正在实现一个程序,它将消息隐藏在 ppm 文件中,然后检索它们。我需要做的最后一件事是,如果打开文件出现问题,则“返回 2”。如果我输入一个无法打开的文件(文件不存在或扩展名错误等),那么它将在 stderr 上显示错误消息,但由于某种原因它不会执行下一行,“return 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;
}
<小时/>

编辑:

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;
}
}

最佳答案

程序结束,退出代码:0

C 程序的退出代码是显式 exit 函数调用中 main 的返回值或 status 值。在您的情况下,load_ppm_image 返回一个值到其调用函数 hide_message,而该函数又将相同的值返回到其调用函数 main。但是,对于调用 hide_message 的情况,main 不会显式返回任何内容。 C 标准指定,如果 main 到达函数末尾而没有显式返回,则隐式返回 0。因此,在您的情况下,退出代码为 0。

要获得您想要的行为,请修改您的 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]));
}

关于C:永远不会返回语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35931856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com