输出流错误。有人可以调试这个吗?
我希望使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您正在处理
FILE
,我会说这是一个ofstream
错误,而不是ostream
错误。 :)FILE
是一个 C 风格的文件 I/O,操作符 <<
没有被重载。您应该使用
ofstream
对象,该对象的运算符 <<
在库中重载。Since you are dealing with
FILE
, I would say it's anofstream
error rather thanostream
error. :)FILE
is a C-style file i/o for whichoperator <<
is not overloaded.You should use
ofstream
object for which theoperator <<
is overloaded in the library.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
你很困惑。在经典的 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