创建一个文本文件并用 C++ 写入它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5362607/
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-28 17:59:54  来源:igfitidea点击:

Create a text file and write to it in C++?

c++file-iotext-files

提问by user637429

I am using Visual C++ 2008. I want to create a text file and write to it.

我使用的是 Visual C++ 2008。我想创建一个文本文件并写入它。

char filename[]="C:/k.txt";
FileStream *fs = new FileStream(filename, FileMode::Create, FileAccess::Write);
fstream *fs =new fstream(filename,ios::out|ios::binary);
fs->write("ghgh", 4);
fs->close();

Here is showing error of FileStream

这是显示 FileStream 的错误

回答by Matteo Italia

You get an error because you have fsdeclared twice in two different ways; but I wouldn't keep anything of that code, since it's a strange mix of C++ and C++/CLI.

你得到一个错误,因为你fs以两种不同的方式声明了两次;但我不会保留任何代码,因为它是 C++ 和 C++/CLI 的奇怪组合。

In your question is not clear whether you want to do standard C++ or C++/CLI; assuming you want "normal" C++, you should do:

在您的问题中不清楚您是要执行标准 C++ 还是 C++/CLI;假设你想要“普通”C++,你应该这样做:

#include <fstream>
#include <iostream>

// ...

int main()
{
    // notice that IIRC on modern Windows machines if you aren't admin 
    // you can't write in the root directory of the system drive; 
    // you should instead write e.g. in the current directory
    std::ofstream fs("c:\k.txt"); 

    if(!fs)
    {
        std::cerr<<"Cannot open the output file."<<std::endl;
        return 1;
    }
    fs<<"ghgh";
    fs.close();
    return 0;
}

Notice that I removed all the newstuff since very often you don't need it in C++ - you can just allocate the stream object on the stack and forget about the memory leaks that were present in your code, since normal (non GC-managed) pointers aren't subjected to garbage collection.

请注意,我删除了所有的new东西,因为在 C++ 中你经常不需要它——你可以只在堆栈上分配流对象,而忘记代码中存在的内存泄漏,因为正常(非 GC 管理)指针不受垃圾收集的影响。

回答by David Cravey

Here are examples for both native and managed C++:

以下是本机和托管 C++ 的示例:

Assuming you are happy with a native solution the following works just fine:

假设您对本机解决方案感到满意,以下工作正常:

    fstream *fs =new fstream(filename,ios::out|ios::binary); 
fs->write("ghgh", 4); 
fs->close(); 
delete fs;      // Need delete fs to avoid memory leak

However, I would not use dynamic memory for the fstream object (i.e. the new statement and points). Here is the new version:

但是,我不会为 fstream 对象(即新语句和点)使用动态内存。这是新版本:

    fstream fs(filename,ios::out|ios::binary); 
fs.write("ghgh", 4); 
fs.close();

EDIT, the question was edited to request a native solution (originally it was unclear), but I will leave this answer as it may be of use to someone

编辑,问题被编辑以请求本地解决方案(最初不清楚),但我会留下这个答案,因为它可能对某人有用

If you are looking for a C++CLI option (for managed code), I recommend using StreamWriter instead of FileStream. StreamWriter will allow you work with Managed Strings. Note that delete will call the Dispose method on the IDisposable interface and the Garbage Collected will release the memory eventually:

如果您正在寻找 C++CLI 选项(用于托管代码),我建议使用 StreamWriter 而不是 FileStream。StreamWriter 将允许您使用托管字符串。注意 delete 会调用 IDisposable 接口上的 Dispose 方法,Garbage Collected 最终会释放内存:

StreamWriter ^fs = gcnew StreamWriter(gcnew String(filename));
fs->Write((gcnew String("ghgh")));
fs->Close();
delete fs;

回答by Amad Munir

you create a text. Ask the user if he would like to send it. If he says yes, this means that this particular message should be tagged as outbox message otherwise it should be an inbox message.

你创建一个文本。询问用户是否愿意发送。如果他说是,这意味着这个特定的消息应该被标记为发件箱消息,否则它应该是收件箱消息。