我遇到一个简单的fwrite问题
一点背景,基本上我正在使用Base64格式的Cassandra表中恢复和图片数据,我使用的解码功能非常好,然后我将这些数据写入“文件” “.jpg,我知道它正常工作,因为这个算法适用于55 Kb的图像,但我有一个问题,但是当我尝试从大图像中读取时,我的意思是一个优于100Kb的图像(我是用不同的图像做了多次测试)。
我的问题是fwrite(使用小图片)无效并且返回0并且perror状态为:Bad Adress。
这是我的代码,如果您需要更多信息,请随时提出任何问题! :
while(cass_iterator_next(rows))
//everything before this is not really important. Every Cass thing are unique to cassandra and are working !
{
const CassRow* row = cass_iterator_get_row(rows);
const CassValue* value = cass_row_get_column_by_name(row, "content");
CassString keyspace_name;
cass_value_get_string(value, &keyspace_name);
//hoolo is a char*, base64 allocate the memory for hoolo77
//fp is a FILE*.
hoolo = base64_decode(keyspace_name.data, (int)keyspace_name.length, &hollo);
fp = fopen("test.jpg", "w+");
int n = fwrite(hoolo, 1, (int)keyspace_name.length / 4 * 3, fp);
//I'm doing the x / 4 * 3 operation to have the size of the converted string length
perror("Error :");
printf("write !!! : %d\n", n);
// N = 0 !
fclose(fp);
}
这里有所有定义:
char *hoolo;
size_t *hollo = 0;
这是Base64_decode
unsigned char *base64_decode(const char *data,
size_t input_length,
size_t *output_length) {
if (decoding_table == NULL)
build_decoding_table();
printf("%s\n", data);
if (input_length % 4 != 0){
return NULL;
}
printf("%d\n", (int)input_length);
*output_length = input_length / 4 * 3;
if (data[input_length - 1] == '=') (*output_length)--;
if (data[input_length - 2] == '=') (*output_length)--;
unsigned char *decoded_data = malloc(*output_length);
if (decoded_data == NULL) return NULL;
for (int i = 0, j = 0; i < input_length;) {
uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
uint32_t triple = (sextet_a << 3 * 6)
+ (sextet_b << 2 * 6)
+ (sextet_c << 1 * 6)
+ (sextet_d << 0 * 6);
if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
}
return decoded_data;
}
这是BT,当我尝试Printf hoolo
#0 strlen () at ../sysdeps/x86_64/strlen.S:106
#1 0x00007ffff779a8bc in _IO_puts (str=0xfffffffff7eb3010 <error: Cannot access memory at address 0xfffffffff7eb3010>) at ioputs.c:36
#2 0x0000000000401381 in main () at select.c:47