在 ms Visual c++ 中的按钮单击事件中使用文件流
我知道你必须包含 fstream 库才能使用。假设我有一个名为 Sample_project 的 Visual C++ Windows 窗体项目。当然,这将有一个名为sample_project.cpp 的主源文件。
我已放置
include <fstream>
在sample_project.cpp 文件中,但在表单上的按钮单击事件中调用文件流函数仍然不起作用。这可能是我在这里缺少的基本内容,但任何帮助将不胜感激。
I know that you have to include the fstream library in order to use. Let's say I have a windows form project in visual c++ named sample_project. And of course that would have a main source file named sample_project.cpp.
I have placed
include <fstream>
on the sample_project.cpp file but calling the filestream functions in the button click event on my form still does not work. This is probably basic stuff that I am missing here but any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不能确定,因为您忘记从尝试调用文件流函数的按钮单击事件处理程序中发布代码,但我的猜测是您忘记了使用适当的命名空间来限定函数调用。
该头文件中定义的函数位于
std
命名空间中,因此您必须编写:或者在代码文件的顶部添加一个 using 指令:
另外,我假设您刚刚遇到了问题代码格式化功能,但请确保您的 include 语句实际上如下所示,注意
#
符号以及尖括号周围缺少空格:请记住 Windows 窗体项目面向 .NET Framework,旨在促进本机代码和托管代码之间的互操作性。如果您打算编写纯非托管 C++ 代码,则可以创建控制台或 Win32 应用程序。如果您不需要 fstream.h 提供的特定功能,您可以使用 .NET Framework 中包含的标准文件操作类进行研究。它们虽然没有那么强大,但在大多数情况下已经足够了,而且对于初学者来说使用起来要简单得多。
I can't tell for certain, because you forgot to post the code from your button click event handler where you try to call the filestream functions, but my guess is that you've forgotten to qualify the function calls with the appropriate namespace.
The functions defined in that header file are in the
std
namespace, so you would have to write:Or add a using directive to the top of your code file:
Also, I assume you just had problems with the code formatting feature, but make sure that your include statement actually looks like this, noting the
#
sign and the lack of space around the angle brackets:Remember that a Windows Forms project targets the .NET Framework, and is designed to facilitate interoperability between native and managed code. If you intended to write pure, unmanaged C++ code, you create a console or Win32 application, instead. And if you don't need the specific functionality provided by
fstream.h
, you could investigate using the standard file manipulation classes included with the .NET Framework. They're not quite as powerful, but they are more than adequate in the majority of cases, and far simpler to use for a beginning programmer.