<bdo id='JGSHH'></bdo><ul id='JGSHH'></ul>
<legend id='JGSHH'><style id='JGSHH'><dir id='JGSHH'><q id='JGSHH'></q></dir></style></legend><tfoot id='JGSHH'></tfoot>

      <small id='JGSHH'></small><noframes id='JGSHH'>

      1. <i id='JGSHH'><tr id='JGSHH'><dt id='JGSHH'><q id='JGSHH'><span id='JGSHH'><b id='JGSHH'><form id='JGSHH'><ins id='JGSHH'></ins><ul id='JGSHH'></ul><sub id='JGSHH'></sub></form><legend id='JGSHH'></legend><bdo id='JGSHH'><pre id='JGSHH'><center id='JGSHH'></center></pre></bdo></b><th id='JGSHH'></th></span></q></dt></tr></i><div id='JGSHH'><tfoot id='JGSHH'></tfoot><dl id='JGSHH'><fieldset id='JGSHH'></fieldset></dl></div>

        使用 C++ 读取文本文件的最优雅方式是什么?

        What is the most elegant way to read a text file with c++?(使用 C++ 读取文本文件的最优雅方式是什么?)
      2. <small id='u1RGe'></small><noframes id='u1RGe'>

            <i id='u1RGe'><tr id='u1RGe'><dt id='u1RGe'><q id='u1RGe'><span id='u1RGe'><b id='u1RGe'><form id='u1RGe'><ins id='u1RGe'></ins><ul id='u1RGe'></ul><sub id='u1RGe'></sub></form><legend id='u1RGe'></legend><bdo id='u1RGe'><pre id='u1RGe'><center id='u1RGe'></center></pre></bdo></b><th id='u1RGe'></th></span></q></dt></tr></i><div id='u1RGe'><tfoot id='u1RGe'></tfoot><dl id='u1RGe'><fieldset id='u1RGe'></fieldset></dl></div>
          • <legend id='u1RGe'><style id='u1RGe'><dir id='u1RGe'><q id='u1RGe'></q></dir></style></legend>
            <tfoot id='u1RGe'></tfoot>

              • <bdo id='u1RGe'></bdo><ul id='u1RGe'></ul>

                    <tbody id='u1RGe'></tbody>
                  本文介绍了使用 C++ 读取文本文件的最优雅方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想用 C++ 将文本文件的全部内容读取到 std::string 对象.

                  I'd like to read whole content of a text file to a std::string object with c++.

                  使用 Python,我可以编写:

                  With Python, I can write:

                  text = open("text.txt", "rt").read()
                  

                  它非常简单和优雅.我讨厌丑陋的东西,所以我想知道 - 用 C++ 读取文本文件的最优雅的方式是什么?谢谢.

                  It is very simple and elegant. I hate ugly stuff, so I'd like to know - what is the most elegant way to read a text file with C++? Thanks.

                  推荐答案

                  方法很多,你挑最适合你的.

                  There are many ways, you pick which is the most elegant for you.

                  读入字符*:

                  ifstream file ("file.txt", ios::in|ios::binary|ios::ate);
                  if (file.is_open())
                  {
                      file.seekg(0, ios::end);
                      size = file.tellg();
                      char *contents = new char [size];
                      file.seekg (0, ios::beg);
                      file.read (contents, size);
                      file.close();
                      //... do something with it
                      delete [] contents;
                  }
                  

                  进入 std::string:

                  Into std::string:

                  std::ifstream in("file.txt");
                  std::string contents((std::istreambuf_iterator<char>(in)), 
                      std::istreambuf_iterator<char>());
                  

                  转化为向量<字符>:

                  std::ifstream in("file.txt");
                  std::vector<char> contents((std::istreambuf_iterator<char>(in)),
                      std::istreambuf_iterator<char>());
                  

                  转成字符串,使用stringstream:

                  Into string, using stringstream:

                  std::ifstream in("file.txt");
                  std::stringstream buffer;
                  buffer << in.rdbuf();
                  std::string contents(buffer.str());
                  

                  file.txt 只是一个例子,对于二进制文件一切正常,只要确保在 ifstream 构造函数中使用 ios::binary.

                  file.txt is just an example, everything works fine for binary files as well, just make sure you use ios::binary in ifstream constructor.

                  这篇关于使用 C++ 读取文本文件的最优雅方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to print vector#39;s data(如何打印矢量的数据)
                  Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                  How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                  MSVC++ warning flags(MSVC++ 警告标志)
                  How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                  stack overflow error in C++ program(C++程序中的堆栈溢出错误)
                1. <i id='tW33x'><tr id='tW33x'><dt id='tW33x'><q id='tW33x'><span id='tW33x'><b id='tW33x'><form id='tW33x'><ins id='tW33x'></ins><ul id='tW33x'></ul><sub id='tW33x'></sub></form><legend id='tW33x'></legend><bdo id='tW33x'><pre id='tW33x'><center id='tW33x'></center></pre></bdo></b><th id='tW33x'></th></span></q></dt></tr></i><div id='tW33x'><tfoot id='tW33x'></tfoot><dl id='tW33x'><fieldset id='tW33x'></fieldset></dl></div>
                  <legend id='tW33x'><style id='tW33x'><dir id='tW33x'><q id='tW33x'></q></dir></style></legend>

                    <tfoot id='tW33x'></tfoot>

                  1. <small id='tW33x'></small><noframes id='tW33x'>

                      <tbody id='tW33x'></tbody>
                        <bdo id='tW33x'></bdo><ul id='tW33x'></ul>