本文介绍了在C ++中创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C ++创建一个文件,但我不知道如何做。例如,我想创建一个名为 Hello.txt 的文本文件。

I want to create a file using C++, but I have no idea how to do it. For example I want to create a text file named Hello.txt.

任何人都可以帮助我吗?

Can anyone help me?

推荐答案

是创建一个ofstream类的实例,并使用它来写入你的文件。这里有一个指向一个网站的链接,其中包含一些示例代码,以及一些有关C ++大多数实现的标准工具的更多信息:

One way to do this is to create an instance of the ofstream class, and use it to write to your file. Here's a link to a website that has some example code, and some more information about the standard tools available with most implementations of C++:

为了完整起见,以下是一些示例代码:

For completeness, here's some example code:

// using ofstream constructors.
#include <iostream>
#include <fstream>

std::ofstream outfile ("test.txt");

outfile << "my text here!" << std::endl;

outfile.close();

您要使用std :: endl结束行。另一种方法是使用\\\
字符。这两个东西是不同的,std :: endl刷新缓冲区并立即写入你的输出,而'\\\
'允许outfile把你的所有输出到一个缓冲区,也许以后写。

You want to use std::endl to end your lines. An alternative is using '\n' character. These two things are different, std::endl flushes the buffer and writes your output immediately while '\n' allows the outfile to put all of your output into a buffer and maybe write it later.

这篇关于在C ++中创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:54