在 C++ 中创建文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/478075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:33:15  来源:igfitidea点击:

Creating files in C++

c++file-io

提问by Uffo

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.

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

Can anyone help me?

谁能帮我?

回答by James Thompson

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++:

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

ofstream reference

流参考

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();

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.

您想使用 std::endl 来结束您的行。另一种方法是使用 '\n' 字符。这两件事是不同的, std::endl 刷新缓冲区并立即写入您的输出,而 '\n' 允许输出文件将所有输出放入缓冲区,并可能稍后写入。

回答by Boiethios

Do this with a file stream. When a std::ofstreamis closed, the file is created. I personally like the following code, because the OP only asks to create a file, not to write in it:

使用文件流执行此操作。当 astd::ofstream关闭时,文件被创建。我个人喜欢下面的代码,因为 OP 只要求创建一个文件,而不是在其中写入:

#include <fstream>

int main()
{
    std::ofstream file { "Hello.txt" };
    // Hello.txt has been created here
}

The temporary variable fileis destroyed right after its creation, so the stream is closed and thus the file is created.

临时变量file在创建后立即销毁,因此流被关闭,从而创建文件。

回答by Sean Bright

#include <iostream>
#include <fstream>

int main() {
  std::ofstream o("Hello.txt");

  o << "Hello, World\n" << std::endl;

  return 0;
}

回答by George Theodosiou

Here is my solution:

这是我的解决方案:

#include <fstream>

int main()
{
    std::ofstream ("Hello.txt");
    return 0;
}

File (Hello.txt) is created even without ofstream name, and this is the difference from Mr. Boiethios answer.

文件(Hello.txt)即使没有ofstream名称也会创建,这与Boiethios先生的回答不同。

回答by Angelo

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

string filename = "/tmp/filename.txt";

int main() {
  std::ofstream o(filename.c_str());

  o << "Hello, World\n" << std::endl;

  return 0;
}

This is what I had to do in order to use a variable for the filename instead of a regular string.

为了使用文件名变量而不是常规字符串,我必须这样做。

回答by Angelo

use c methods FILE *fp =fopen("filename","mode");fclose(fp);mode means a for appending r for reading ,w for writing

使用 c 方法FILE *fp =fopen("filename","mode");fclose(fp);模式意味着 a 用于附加 r 用于读取,w 用于写入

   / / using ofstream constructors.
      #include <iostream>
       #include <fstream>  
      std::string input="some text to write"
     std::ofstream outfile ("test.txt");

    outfile <<input << std::endl;

       outfile.close();

回答by Vinayyy

/*I am working with turbo c++ compiler so namespace std is not used by me.Also i am familiar with turbo.*/

#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<fstream.h> //required while dealing with files
void main ()
{
clrscr();
ofstream fout; //object created **fout**
fout.open("your desired file name + extension");
fout<<"contents to be written inside the file"<<endl;
fout.close();
getch();
} 

After running the program the file will be created inside the bin folder in your compiler folder itself.

运行程序后,该文件将在编译器文件夹本身的 bin 文件夹中创建。