简体   繁体   English

用C ++创建和写入文件

[英]Create and write in a file in c++

I want to create a file and write something inside. 我想创建一个文件并在其中写一些东西。 so I wrote this: 所以我这样写:

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, const char * argv[])
{
    ofstream m;
    m.open("bonjour.txt");
    m << "bonjour.\n";
    m.close();
    return 0;
}

And I think that this is correct, but when I execute it, I find no file bonjour.txt. 而且我认为这是正确的,但是当我执行它时,没有找到文件bonjour.txt。 Have I done something wrong? 我做错什么了吗?

There is no issue with the code. 代码没有问题。 It works for me and file "bonjour.txt" created with content "bonjour". 它对我有用,并且文件“ bonjour.txt”与内容“ bonjour”一起创建。

ofstream m("bonjour.txt", ios_base::app);

with the use of the append mode you can open and close your file as many times as you want and it won't erase the content that was in the text file previously. 使用附加模式,您可以根据需要打开和关闭文件多次,并且不会删除文本文件中以前的内容。 This is reference to the ios_base::openmodes http://www.cplusplus.com/reference/ios/ios_base/openmode/ 这是对ios_base :: openmodes的引用http://www.cplusplus.com/reference/ios/ios_base/openmode/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM