fstream 到 const char *
我想要做的是读取一个名为“test.txt”的文件,然后让该文件的内容为 const char * 类型。一个人会怎样做呢?
What I want to do is read a file called "test.txt", and then have the contents of the file be a type const char *. How would one do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意不要显式调用数据删除
Be careful not to explicitly call delete on data
您不太可能真的想这样做。文件的内容(可能是文本或二进制数据)不太可能表示指向体系结构上的字符的(有效)指针,因此将其[内容]表示为
并没有真正的意义常量字符*
。您可能想要的是将文件的内容加载到内存中,然后将指针(类型为 const char*)存储到给定块的开头。 实现这一目标的一种方法是:
It's highly unlikely you really want to do that. The contents of the file (which may be either text, or binary data) are unlikely to represent a (valid) pointer to a char on your architecture, so it is not really meaningful to represent it [the content] as a
const char *
.What you may instead want is to load the contents of the file in memory, and then store a pointer (of type const char*) to the beginning of the given block. </pedantry> One way of achieving that:
您需要:
fstream
tell
) 确定文件You need to:
fstream
on the filetell
)