【发布时间】:2016-01-14 20:55:08
【问题描述】:
我不明白为什么当我在无限循环中打印它时会打印正确的结果,但当我在没有循环的情况下打印它时不打印任何东西。
这是我的代码:
#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;
}
让我们专注于(在 main() 接近尾声)
while (1) {
strcpy(received, decodeReverse(argv, argc));
printf("Received : %s ",received);
}
通过上面的代码,我得到了正确的输出:
Received : Hey
Received : Hey
Received : Hey
Received : Hey
Received : Hey
但如果打印 received 没有无限的 while 循环,我会得到:
Received :
好像received 是空的。
编辑:
我清理了代码并应用了修复程序,但如果没有无限循环,它仍然无法工作(即使进行了几次迭代)。
谢谢你们!
【问题讨论】:
-
请发MCVE
-
int main(int argc, const char * argv[]) {不是main的有效原型,请使用int main(int argc, char * argv[]) { -
首先修复@mksteve 和@Magisch 指出的错误,例如将
received缓冲区从外部传递给decodeReverse(),最好作为调用参数。但这并不能解释为什么在单次调用时解码失败,但在迭代时它可以工作。有没有我们还没有发现的副作用?尝试只运行两到三次迭代而不是无限循环,然后查看结果。 -
@CiaPan 我尝试了两次和三次迭代,没有打印出来
标签: c function pointers printf