【问题标题】:Is there a C++ way to write file with any type of data?是否有 C++ 方法来编写包含任何类型数据的文件?
【发布时间】:2010-08-20 14:27:14
【问题描述】:

喜欢 C 中的这个函数:

size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

我查看了 C++ 文件流并找到了这个:

ostream& write ( const char* s , streamsize n );

这个只接受char*而不是void*

但是如果我在 c++ 中使用 C 风格的 fwrite 函数真的很重要吗?

【问题讨论】:

  • 没有什么能阻止你在 C++ 程序中使用fwrite。但请确保包含适当的标题。但是,在 C++ 中,自定义数据通常是通过重载插入运算符 op<< 来编写的。
  • @dirkgently:是的,但 OP 要求使用“c++ 方式”
  • void 用于表示没有类型。在 C++ 中,指令 sizeof(void) 无法编译。 stackoverflow.com/questions/1666224/what-is-the-size-of-void
  • @John Dibling:这就是评论的原因。

标签: c++ file-io


【解决方案1】:

除非我误解了您的问题,否则您可能正在寻找流。有很多风格可以处理不同的工作,比如输出到文件:

#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;


int main()
{
    ofstream f("c:\\out.txt");

    const char foo[] = "foo";
    string bar = "bar";
    int answer = 42;

    f << foo << bar<< answer;

    return 0;
}

...像使用 printf 一样构建字符串:

#include <cstdlib>
#include <sstream>
#include <string>
#include <iostream>
using namespace std;


int main()
{
    stringstream ss;

    const char foo[] = "foo";
    string bar = "bar";
    int answer = 42;

    ss << foo << bar<< answer;
    string my_out = ss.str();

    return 0;
}

...如果您告诉他们如何处理,他们甚至可以处理您自己的类型:

#include <cstdlib>
#include <string>
#include <iostream>
using namespace std;

class MyGizmo
{
public:
    string bar_;
    int answer_;

    MyGizmo() : bar_("my_bar"), answer_(43) {};
};

ostream& operator<<(ostream& os, const MyGizmo& g)
{
    os << g.bar_ << " = " << g.answer_;
    return os;
}
int main()
{
    MyGizmo gizmo;
    cout << gizmo;

    return 0;
}

【讨论】:

    【解决方案2】:

    您可以使用任何一种。使用 char * 而不是 void * 并没有太大的区别——fwriteostream::write 通常用于各种数据类型(使用 C++,您需要向 @987654325 添加显式转换@,假设您已经为fwrite 包含了适当的原型,那么在 C 中转换将隐式发生。

    【讨论】:

    • 只要确保您使用 std::ios::bin 标志打开了流。
    【解决方案3】:

    在 C++ 中,您将需要使用 std::ofstream 对象来写入文件。他们可以使用&lt;&lt; 运算符接受任何类型的数据,这与std::cout 用于写入控制台的方式大致相同。当然,和std::cout一样,如果要打印自定义类型,就需要为它定义一个operator&lt;&lt;重载。

    一个例子:

    std::ofstream outfile("myfile.txt");
    
    int i = 5;
    double d = 3.1415926535898;
    std::string s = "Hello, World!";
    
    outfile << i << std::endl;
    outfile << d << std::endl;
    outfile << s << std::endl;
    

    要使用std::ofstream,您需要#include &lt;fstream&gt;

    outfile对象在销毁时会自动关闭文件,也可以调用它的close()方法。

    【讨论】:

      【解决方案4】:

      与已经给出的答案相反,fwrite() 和 ostream::write() 之间有一个重要区别。

      fwrite() 写入未修改的二进制数据(好吧,在那些糟糕的非 Unix 平台上,存在端行转换,除非文件以二进制模式打开)。

      ostream::write() 使用语言环境来转换每个字符,这就是它接受 char* 而不是 void* 的原因。通常,它使用默认的“C”语言环境,不进行任何转换。

      请记住,basic_ostream 是 basic_streambuf 之上的格式化程序,而不是二进制接收器。

      【讨论】:

      • fwrite 如果您以二进制模式打开文件,则写入二进制数据,如果您以文本模式打开文件,则将其写入为文本。但在某些系统上,文本模式和二进制模式没有区别。
      • 老兄,你应该在评论之前阅读我的帖子:...(好吧,在那些糟糕的非 Unix 平台上,除非文件以二进制模式打开,否则会有尾行翻译)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多