fstream 到 const char *

发布于 2024-09-28 19:49:23 字数 69 浏览 4 评论 0原文

我想要做的是读取一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

执笔绘流年 2024-10-05 19:49:23
#include <string>
#include <fstream>

int main()
{
   std::string line,text;
   std::ifstream in("test.txt");
   while(std::getline(in, line))
   {
       text += line + "\n";
   }
   const char* data = text.c_str();
}

注意不要显式调用数据删除

#include <string>
#include <fstream>

int main()
{
   std::string line,text;
   std::ifstream in("test.txt");
   while(std::getline(in, line))
   {
       text += line + "\n";
   }
   const char* data = text.c_str();
}

Be careful not to explicitly call delete on data

幻想少年梦 2024-10-05 19:49:23

您不太可能真的想这样做。文件的内容(可能是文本或二进制数据)不太可能表示指向体系结构上的字符的(有效)指针,因此将其[内容]表示为 并没有真正的意义常量字符*

您可能想要的是将文件的内容加载到内存中,然后将指针(类型为 const char*)存储到给定块的开头。 实现这一目标的一种方法是:

#include <sstream>
#include <fstream>
// ...
{
    std::ostringstream sstream;
    std::ifstream fs("test.txt");
    sstream << fs.rdbuf();
    const std::string str(sstream.str());
    const char* ptr = str.c_str();
    // ptr is the pointer we wanted - do note that it's only valid
    // while str is valid (i.e. not after str goes out of scope)
}

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:

#include <sstream>
#include <fstream>
// ...
{
    std::ostringstream sstream;
    std::ifstream fs("test.txt");
    sstream << fs.rdbuf();
    const std::string str(sstream.str());
    const char* ptr = str.c_str();
    // ptr is the pointer we wanted - do note that it's only valid
    // while str is valid (i.e. not after str goes out of scope)
}
旧竹 2024-10-05 19:49:23

您需要:

  1. 创建一个返回 const char* 的函数
  2. 在文件上打开一个 fstream
  3. 查找文件末尾
  4. 通过查看文件位置 (tell) 确定文件
  5. 长度回到开头
  6. 创建一个 char* 来包含文件内容
  7. 将文件内容读入 char*
  8. 返回 char* 指针,函数 return 添加 const
  9. 文件会因 fstream 超出范围而自动关闭

You need to:

  1. create a function returning a const char*
  2. open an fstream on the file
  3. seek to its end
  4. determine the file length by looking at the file position (tell)
  5. seek back to the beginning
  6. create a char* to contain the file contents
  7. read the file contents into the char*
  8. return the char* pointer, with the function return adding the const
  9. the file is closed automatically by the fstream going out of scope
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文