对于 char * varable,如何在调试模式下查看其值
For char * varable, how to view its value in debug mode
我在下面编写了代码,通过VC2017回读二进制文件。在调试模式下,我喜欢看到"缓冲区"中的值。但我看不到可读的值。我的问题是:
- 如何查看可读结果? 我
- 做了"sizeof(buffer)",它返回了 4,这比我预期的要少。我希望缓冲区的大小和文件大小相同。为什么?
非常感谢你启发我。
char* read_back(const char* filename)
{
FILE* pFile;
long lSize;
char* buffer;
pFile = fopen(filename, "rb");
if (pFile == NULL)
{
fputs("File error", stderr);
exit(1);
}
fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile); // set file pos at the begining
// copy the file into the buffer:
buffer = (char*)malloc(sizeof(char)*lSize);
size_t result = fread(buffer, 1, lSize, pFile);
if (result != lSize)
{
fputs("Reading error", stderr);
exit(3);
}
fclose(pFile);
return buffer;
}
下面介绍如何用C++编写代码(不是你实际拥有的 C 代码)
#include <fstream>
#include <sstream>
#include <string>
std::string read_back(const char* filename)
{
std::ifstream file(filename, std::ios_base::binary);
std::ostringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
它返回一个 std::string 而不是一个 char*,但这是一件好事,因为您没有记住必须释放分配的内存的问题。
如前所述,您误解了指针和大小的工作方式。避免指针,它们很难。
相关文章:
- 为什么使用__LINE_的代码在发布模式下在MSVC下编译,而不是在调试模式下
- 小字符串优化(调试与发布模式)
- 在调试模式下引发C++ "deque iterator not dereferencable"异常
- _mm256_load_ps调试模式下导致谷歌/基准测试的分段错误
- Visual Studio 发布模式阻止在调试模式下执行的代码.使用 WinHTTP 和多线程
- 自定义内存管理器在发布模式下工作正常,但在调试模式下则不然
- 使用Qt Creator在调试模式下编译一段代码
- 在调试模式下,所有程序都运行良好,但在发布模式下,我在cudaMalloc操作中出错
- 在类声明中初始化 const 成员变量时在调试模式下出现异常
- 在调试模式下编译时qrc_resource.cpp错误
- 如何在调试模式下通过终端运行可执行的 c++?
- 在Visual Studio 2017上无法使用调试模式x64进行OpenCV3.2编译
- 在调试模式下使用英特尔 TBB
- 我的代码在发布模式下不起作用,而在Qt的调试模式下工作
- SCANF 在 Eclipse 中不接受调试模式下的输入?
- 为什么我的 UWP 游戏在发布时比在调试模式下慢
- 在发布模式下崩溃,但如果可调试为 true - 不是..什么是可能的问题
- Visual Studio混合模式调试是否可以附加到Jupyter笔记本以同时调试C++和Python
- Windows 用户模式调试器传输 Visual Studio 2012
- VS2010 中使用 .NET 3.5 的混合模式调试