一个strlen引发的血案

部分测试代码原来是这样的:

int DecryptRelation::AesDecryptFromFileToBytes(const std::string in_file_path, unsigned char *out_data,
											   const char* aes_encrypt_key, int in_data_chunk_size)
{
	ifstream fin(in_file_path, ios::binary);

	ofstream fout("c:\\test.txt", ios::binary);
	

	if (!fin)
	{
		return 1;
	}

	int out_data_position = 0;
	char *in_data  = new char[in_data_chunk_size + 1];
	char *in_data_tmp  = new char[in_data_chunk_size + 1];
	while (!fin.eof())
	{
		fin.read(in_data, in_data_chunk_size);

		if (fin.gcount() >= in_data_chunk_size)
		{
			AesDecryptBytes((const unsigned char *)in_data, (unsigned char *)in_data_tmp, 
				aes_encrypt_key, in_data_chunk_size);
			for (int in_data_tmp_position = 0; in_data_tmp_position < fin.gcount(); in_data_tmp_position++)
			{
				out_data[out_data_position++] =  in_data_tmp[in_data_tmp_position];
			}
		}
		else
		{
			for (int in_data_tmp_position = 0; in_data_tmp_position < fin.gcount(); in_data_tmp_position++)
			{
				out_data[out_data_position++] =  in_data[in_data_tmp_position];
			}
		}
	}

	int str_length = strlen((const char *)out_data);
	fout.write((const char *)out_data, str_length);
	fout.close();

	// 测试写到文件中
	fin.close();

	RELESE_ARRAY(in_data);
	RELESE_ARRAY(in_data_tmp);

	if (out_data_position == 0)
	{
		return 1;
	}

	return 0;
}

注意这里:

int str_length = strlen((const char *)out_data);
	fout.write((const char *)out_data, str_length);
	fout.close();

查看c盘对应的文件大小只有:



当时居然没发现,文件里面带有\0导致的问题。。


做个测试,发现还真是这样。。

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "string"
#include "iostream"
using namespace std;
#include "fstream"
#include "decrypt_relation.h"
#include "TxtAndFlashDecryptShow.h"


void Test(unsigned char* in_data)
{
	int str_length = strlen((const char *)in_data);
	printf("Test----------%s\n", in_data);
	printf("Test----------%d\n", str_length);
}

int _tmain(int argc, _TCHAR* argv[])
{
// 	ifstream fin("1.txt", ios::binary);
// 	fin.seekg(0, ios::end);
// 	int file_length = fin.tellg();
// 	fin.close();
// 
// 	char *test = new char[file_length];
// 
// 	DecryptRelation::AesDecryptFromFileToBytes("2.txt", (unsigned char *)test, 
// 												"0123456789abcdef0123456789abcdee");
// 
// 	printf("file str length is : %d\n", strlen(test));
// 
// 	if (test)
// 	{
// 		delete test;
// 		test = NULL;
// 	}
	
	TxtAndFlashDecryptShow *test = new TxtAndFlashDecryptShow();
	test->ParaseText("2.txt", "0123456789abcdef0123456789abcdee", "");

	char* str_point = new char[100];
	strcpy(str_point, "zeg\0test");
	printf("main---------%s\n", str_point);
	printf("main---------%d\n", strlen((const char *)str_point));

	Test((unsigned char*)str_point);

//	char *test = "zengraoli";
//	printf("%d\n", strlen(test));
//	ofstream fout("2.txt", ios::binary);
//	fout.write(test, strlen(test));

//	printf("file str length is : %d\n", strlen(test->GetOutData()));

	return 0;
}



完了完了,这么基础的问题都忘了。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值