Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建一个文本文件并用C++;? 我使用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();_C++_File Io_Text Files - Fatal编程技术网

创建一个文本文件并用C++;? 我使用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();

创建一个文本文件并用C++;? 我使用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();,c++,file-io,text-files,C++,File Io,Text Files,这里显示的是FileStream的错误您得到一个错误,因为您用两种不同的方式声明了两次fs;但是我不会保留任何代码,因为它是C++和C++ +CLI的奇怪组合。 你的问题不清楚你是否想做标准的C++或C++/CLI;假设你想要“正常”C++,你应该做: #include <fstream> #include <iostream> // ... int main() { // notice that IIRC on modern Windows machines

这里显示的是FileStream的错误

您得到一个错误,因为您用两种不同的方式声明了两次
fs
;但是我不会保留任何代码,因为它是C++和C++ +CLI的奇怪组合。 你的问题不清楚你是否想做标准的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;
}
#包括
#包括
// ...
int main()
{
//请注意,如果您不是管理员,现代Windows计算机上的IIRC
//无法写入系统驱动器的根目录;
//您应该改为在当前目录中写入
流fs的std::(“c:\\k.txt”);
如果(!fs)
{

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

假设您对本机解决方案感到满意,那么以下方法就可以了:

    fstream *fs =new fstream(filename,ios::out|ios::binary); 
fs->write("ghgh", 4); 
fs->close(); 
delete fs;      // Need delete fs to avoid memory leak
但是,我不会对fstream对象(即新语句和点)使用动态内存。以下是新版本:

    fstream fs(filename,ios::out|ios::binary); 
fs.write("ghgh", 4); 
fs.close();
EDIT,该问题被编辑为请求本机解决方案(最初不清楚),但我将保留此答案,因为它可能对某些人有用

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

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

您创建了一条文本。询问用户是否要发送该文本。如果他说是,这意味着此特定邮件应标记为发件箱邮件,否则它应为收件箱邮件。

请编辑您的帖子并添加您收到的确切错误信息。还有完整的代码(格式正确,带有标题)“C++:java C++文件的代码是什么?”用户:“代码”> FiestReMase>代码是从哪里来的?为什么要创建两个流?为什么要动态创建流?你是一个Java程序员,转换为C++?文件流?这是.NET类吗?你是想做C++还是C++/CLI?那么在C++中?n,你想做“正常”的C++还是C++ + CLI(.NET C++)?可以并且应该(只需在堆栈上分配)问题清楚地关于“代码> .txt < /Case>文件,而不是SMS消息。这个答案没有涉及文本文件或Visual C++ 2008。@ AAMD MunIR在发布答案时所想的是什么?