我正在实施一个程序,该程序隐藏ppm文件中的消息,然后检索它们。我需要做的最后一件事就是"返回2"如果打开文件有问题。如果我输入的文件无法打开(文件不存在或扩展错误等),那么它将在stderr上显示错误消息,但由于某种原因它不会执行下一行,"返回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 :(得分:1)
Program ended with exit code: 0
C程序的退出代码是显式main
函数调用中status
或exit
值的返回值。在您的情况下,load_ppm_image
返回一个值给它的调用函数hide_message
,后者又将相同的值返回给它的调用函数main
。但是,main
没有为调用hide_message
的情况显式返回任何内容。 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]));
}
答案 1 :(得分:0)
来自代码:
在main函数中,您调用hide_message函数,但不收集hide_message()的返回值
hide_message(argv[2], argv[3], argv[4]);
因此主函数以return 0结束。