输出流错误。有人可以调试这个吗?

发布于 2024-12-12 13:26:03 字数 404 浏览 1 评论 0原文

我希望使用 ostream 将以下值写入文件???

int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  pFile<< "r "<<" " <<"ggjjsss" <<'_'<<"gggjj"<< " " << "HLLO " <<endl;
}

我收到以下错误.. 错误:“FILE*”和“const char [3]”类型的操作数对二进制“operator<<”无效

I want the following values to be written into the file using ostream???

int main ()
{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);
    fclose (pFile);
  }
  pFile<< "r "<<" " <<"ggjjsss" <<'_'<<"gggjj"<< " " << "HLLO " <<endl;
}

I am getting the following error..
error: invalid operands of types 'FILE*' and 'const char [3]' to binary 'operator<<'

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

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

发布评论

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

评论(3

凉城已无爱 2024-12-19 13:26:03

由于您正在处理 FILE,我会说这是一个 ofstream 错误,而不是 ostream 错误。 :) FILE 是一个 C 风格的文件 I/O,操作符 << 没有被重载。

您应该使用 ofstream 对象,该对象的运算符 << 在库中重载。

#include<fstream>

ofstream File("myfile.txt");
File<< "r "<<" " <<"ggjjsss" <<'_'<<"gggjj"<< " " << "HLLO " <<endl;

Since you are dealing with FILE, I would say it's an ofstream error rather than ostream error. :) FILE is a C-style file i/o for which operator << is not overloaded.

You should use ofstream object for which the operator << is overloaded in the library.

#include<fstream>

ofstream File("myfile.txt");
File<< "r "<<" " <<"ggjjsss" <<'_'<<"gggjj"<< " " << "HLLO " <<endl;
蓝梦月影 2024-12-19 13:26:03

FILE * 不是 C++ 标准库意义上的 ostream。使用 fstream 代替(或 ofstream)。请参阅此处的示例:cplusplus.com 的 fstream 参考

FILE * is not a ostream in the sense of C++'s standard library. Use fstream instead (or ofstream). Look here for an example: cplusplus.com's fstream reference

万水千山粽是情ミ 2024-12-19 13:26:03

你很困惑。在经典的 C 意义上,“流”只是您可以读取/写入的东西。在 C++ 中,“流”是特殊对象,符合非常特定的接口。

流运算符<<仅在 C++ 中可用,因此不期望它在经典 C 库上工作是很合乎逻辑的

You are confused. In classic C sense "stream" is just something you can read from/write to. In c++ "streams" are specials objects, that comply with very specific interface.

Stream operator << is available only in C++, therefore it would very logical not to expect it to work on classic C libraries

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