C 我的算法的奇怪行为

C 我的算法的奇怪行为,c,function,pointers,printf,C,Function,Pointers,Printf,我不明白为什么在无限循环中打印时会打印正确的结果,而在没有循环的情况下打印时却不会打印任何内容 这是我的密码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> char msg[1000]; time_t get_mti

我不明白为什么在无限循环中打印时会打印正确的结果,而在没有循环的情况下打印时却不会打印任何内容

这是我的密码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>


char msg[1000];


time_t get_mtime(const char *path)
{
    struct stat statbuf;
    if (stat(path, &statbuf) == -1) {
        perror(path);
        exit(1);
    }
    return statbuf.st_mtime;
}


void encode(char* msg, char * argv[], int argc)  {

    const int bmp_header_size = 54;
    int read_code;
    int msg_idx = 0;
    int img_idx = 0;
    int bit_idx = 0;
    char c;
    FILE *img_in = NULL;
    FILE *img_out = NULL;

    if( argc < 3 ){
        printf( "Usage: %s source_bmp output_bmp message.\n", argv[0] );
        exit(1);
    }

    img_in = fopen( argv[3], "rb" );
    if( img_in == NULL ){
        printf( "Could not open the input image file.\n" );
        exit(1);
    }

    img_out = fopen( argv[2], "wb" );
    if( img_out == NULL ){
        printf( "Could not open the output file.\n" );
        exit(1);
    }



    while( ( read_code = fgetc( img_in )) != EOF ){
        c = (char)read_code;

        if( img_idx >= bmp_header_size && msg_idx <= strlen( msg ) ){
            char bit_mask = 1 << bit_idx;

            if( ( msg[msg_idx] & bit_mask) > 0 )
                c |= 1;
            else
                c &= 254;

            bit_idx++;

            if( bit_idx >= 8 ){
                bit_idx = 0;
                msg_idx++;
            }           
        }

        fputc( c, img_out );
        img_idx++;
    }


    fclose(img_in);
    fclose(img_out);

}



char* decode(char * argv[], int argc) {

   /* printf("1 = %s\n", argv[1]);
     printf("2 = %s\n", argv[2]);
     printf("3 = %s\n", argv[3]); */

    const int bmp_header_size = 54;
    const int max_msg_size = 1000;

    int i;
    int c;
    int img_idx = 0;
    int msg_idx = 0;
    int bit_idx = 0;

    FILE *img_in = NULL;

    // char msg[max_msg_size];
    img_in = fopen( argv[2], "rb" );

    if( img_in == NULL ){
        printf( "Could not open the input image file.\n" );
        exit(1);
    }

    for( i=0; i < max_msg_size; i++ )
        msg[i] = 0;

    while( ( c = fgetc( img_in )) != EOF ){
        if( img_idx >= bmp_header_size ){
            char bit_mask = 0;
            if( ( c & 1 )  > 0 )
                bit_mask |= 1 << bit_idx;

            msg[msg_idx] |= bit_mask;

            bit_idx++;

            if( bit_idx >= 8 ){
                if( msg[msg_idx] == '\0' )
                    break;
                bit_idx = 0;
                msg_idx++;
            }
        }

        img_idx++;
    }

    fclose(img_in);
    return msg;

}




int main(int argc, char * argv[]) {

    char message[1000];






    while (1) {


        printf("\n Send : ");
        fgets(message, 1000, stdin);
        encode(message, argv, argc);

        long mod = get_mtime(argv[2]);
        long new = mod;

        while (1) {

            new = get_mtime(argv[2]);

            if (mod != new) {
                break;
            }
        }
        while (1) {
            printf("Received : %s ",decode(argv, argc));
        }


    }

    return 0;
}
通过上面的代码,我得到了正确的输出:

 Received : Hey
 Received : Hey
 Received : Hey
 Received : Hey
 Received : Hey
但是如果在没有无限while循环的情况下接收到print

Received : 
好像收到的
是空的

编辑:

我清理了代码并应用了修复程序,但如果没有无限循环,它仍然无法工作(即使经过几次迭代)


谢谢你们

正在覆盖返回的缓冲区

char msg[max_msg_size];
仅存在于函数中,但正在返回

编译器围绕循环的内存进行跳汰,这意味着它碰巧能够生存,但可能不会在不同的编译器/平台上

char msg[max_msg_size];
是函数
decode
中的局部变量,但您尝试返回它

它与无限循环一起工作的事实与编译器解释循环的方式有关,但它是未定义的行为

要修复此问题,请在
decode
函数外部声明
msg

请记住,您当然也需要对
char*output=msg
delcare在设置msg并返回它之前,首先在函数外部删除它。

请发布
int-main(int-argc,const-char*argv[]){
不是
main
的有效原型,请使用
int-main(int-argc,char*argv[]){
首先修复@mksteve和@Magisch指出的错误,例如通过将
接收的
缓冲区传递给
decodeReverse()
从外部,最好是作为调用参数。但这并不能解释为什么单次调用时解码失败,但迭代时解码会起作用。是否有任何副作用我们还没有发现?请尝试运行两到三次迭代,而不是无限循环,然后查看结果。@CiaPan我尝试了两次和三次迭代,没有打印任何内容这是b概括地重述mksteve的回答,包括典型的“尚未”和“碰巧”。我通常在写我自己的答案时不看其他答案,我经常开始写我自己的,然后做一些事情,然后继续写我自己的。比我的答案更好,因为它还建议如何解决它:-)@Magisch非常感谢你的回答,但不幸的是它仍然不起作用。我声明了msg
char msg[1000]
作为一个全局变量,仍然没有打印…您也对'char*output=msg;return output;'进行了相同的处理,对吗?
char msg[max_msg_size];