在 Windows C++ 中使用 fileIO

发布于 2024-12-31 22:34:54 字数 1274 浏览 2 评论 0原文

我正在尝试在我的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

I am trying to use fileIO in my windows VC++2008 (this is what I have set up for everything I need to do) program, and I have done the following

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

intelisense is working fine, and even gives me the methods for the object, but my compiler throws that it does not recognize ostream even though I know that it resides in the fstream header file that is included.

is there something wrong with my compiler, and how do I check?

additional info

I have done the following, and it now recognizes the stream, but I now get a different error

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.

when I tried to do this as one line with just a constructor I got this issue"

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

when I use the current method my compiler states that

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随心而道 2025-01-07 22:34:54

如果您尝试输出到文件,则可以使用 ofstream

#include <fstream>

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

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

You could use ofstream if you're trying to output to a file:

#include <fstream>

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

The std:: is needed if you're not using the std namespace.

喵星人汪星人 2025-01-07 22:34:54

您尝试过 std::ostream 吗?您可能没有包含名称空间?

Have you tried std::ostream? You might not have included the namespace?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文