简体   繁体   English

无法创建.txt文件并在C ++中写入?

[英]Fail to create a .txt file and write to it in C++?

Inside one of my functions that are being called by the main() , I have the following very simple testing snippet. 在我的一个函数(由main()调用main() ,我有以下非常简单的测试片段。

ofstream outFile;
outFile.open("C:\\Program Files\\data\\test.txt");
outFile << "test\n";
outFile.close();

After running the code, I didn't see the file appears. 运行代码后,我没有看到文件出现。 Why is it so? 为什么会这样?

Unfortunately C++ does not specify any way to get more detailed error. 不幸的是,C ++没有指定任何方法来获得更详细的错误。 See Get std::fstream failure error messages and/or exceptions . 请参阅获取std :: fstream失败错误消息和/或异常

But the platform-specific interface should work. 但是特定于平台的界面应该可行。 After each operation check whether outFile.bad() and if it is true, check GetLastError() . 在每次操作之后检查outFile.bad()是否为true,检查GetLastError() Interpret according to appropriate table in documentation or using FormatMessage . 根据文档中的适当表格或使用FormatMessage


I would suspect the problem is permissions. 我怀疑问题是权限。 Windows Vista introduced this "user access control" that should pop up a dialog whenever program wants to do something that requires administrator privileges even if the current user has them. Windows Vista引入了这种“用户访问控制”,当程序想要执行需要管理员权限的操作时,即使当前用户拥有它们,也应该弹出对话框。 The problem is that the dialog only pops up under certain conditions. 问题是对话框仅在某些条件下弹出。 Notably it will not pop up for console applications and the application is denied permissions right away. 值得注意的是,它不会弹出控制台应用程序,并且应用程序立即被拒绝权限。 Such application has to be explicitly executed "as administrator". 此类应用程序必须“以管理员身份”明确执行。 Of course don't forget being able to write particular file there does not imply being able to create a new one. 当然不要忘记能够编写特定文件并不意味着能够创建新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM