c++ - 在 Windows C++ 中使用 fileIO

标签 c++ windows file-io

我正在尝试在我的 windows VC++2008 程序中使用 fileIO(这是我为我需要做的一切设置的)程序,我已经完成了以下操作

#include <iostream>
#include <fstream>
#include <string>
....
ostream Output;

intelisense 工作正常,甚至为我提供了对象的方法,但我的编译器抛出它无法识别 ostream,即使我知道它位于包含的 fstream 头文件中。

我的编译器有问题吗?我该如何检查?

附加信息

我已经完成了以下操作,它现在可以识别流了,但是我现在得到了一个不同的错误

std::ofstream Output // instead of ostream Output
Output.open("Output/log.txt", ios::out); //so that I can open the file and specify output as ofstream can go both ways.

当我试图只用一个构造函数来完成这一点时,我遇到了这个问题"

1>c:...\engine\gsp420maincore\gsp420maincore\messagequeue.cpp(141) : error C2664: 'std::basic_ostream<_Elem,_Traits>::basic_ostream(std::basic_streambuf<_Elem,_Traits> *,bool)' : cannot convert parameter 1 from 'const char [15]' to 'std::basic_streambuf<_Elem,_Traits> *'
1>        with
1>        [
1>            _Elem=char,
1>            _Traits=std::char_traits<char>
1>        ]
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

当我使用当前方法时,我的编译器指出

error C2653: 'ios' : is not a class or namespace name

最佳答案

如果你想输出到一个文件,你可以使用ofstream:

#include <fstream>

std::ofstream o("file.txt");
o << "Hello File!";
o.close();

如果您不使用 std 命名空间,则需要std::

关于c++ - 在 Windows C++ 中使用 fileIO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9059081/

相关文章:

java - Java中的内存泄漏

c++ - C++程序员学习Objective-C的建议

c# - Process.Start 运行两次

windows - Sphinx:增量合并时权限被拒绝/管道损坏

java - 文件未在 Java 中访问

将字母 append 到文件末尾的 C 程序

c++ - 使用 QMake 进行仅 header 和静态构建

c++ - 移动立方体(C++ 到 C#)

C++ typedef 含义

c++ - 静态断言如果一个 header 包含在另一个 header 中但不包含在 cpp 中