无法在 C++ 中写入文件
我正在尝试最基本的事情......用 C++ 编写一个文件,但该文件没有被写入。我也没有收到任何错误。也许我错过了一些明显的东西......或者什么?
我认为我的代码有问题,但我也尝试了在网上找到的示例,但仍然没有创建文件。
这是代码:
ofstream myfile;
myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt");
myfile << "Writing this to a file.\n";
myfile.close();
我也尝试过事先手动创建文件,但它根本没有更新。
如果这与此有关,我正在运行 Windows 7 64 位。就像完全禁止文件写入操作一样,并且不会显示任何错误消息或异常。
I'm trying to to the most basic of things .... write a file in C++, but the file is not being written. I don't get any errors either. Maybe I'm missing something obvious ... or what?
I thought there was something wrong with my code, but I also tried a sample I found on the net and still no file is created.
This is the code:
ofstream myfile;
myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt");
myfile << "Writing this to a file.\n";
myfile.close();
I've also tried creating the file manually beforehand, but it's not updated at all.
I'm running Windows 7 64bit if that has got something to do with this. It's like file-write operations are completely forbidden and no error messages or exceptions are shown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要以写入模式打开文件:
请务必查看第二个参数的其他选项。例如,如果您正在编写二进制数据,则需要
ios::binary
。您还应该在打开流后检查它:
更新:
AraK 是对的,我忘记了
ofstream
默认情况下处于写入模式,所以这不是问题。也许您根本没有对该目录的写入/创建权限? Win7默认很多目录具有“拒绝全部”的特殊权限。或者该文件可能已经存在并且是只读的?
You need to open the file in write mode:
Make sure to look at the other options for that second argument, as well. If you're writing binary data you'll need
ios::binary
for example.You should also be checking the stream after opening it:
Update:
AraK is right, I forgot that an
ofstream
is in write mode by default, so that's not the problem.Perhaps you simply don't have write/create permissions to the directory? Win7 defaults a lot of directories with special permissions of "deny all". Or perhaps that file already exists and is read-only?
首先扭转斜线。
即使 Windows 也能理解斜杠的反方向。
您可以测试是否有任何错误:
Start off by turning that slash around.
Even Windows understands the slash being the other way around.
You could test if there are any errors:
您是否了解过 Windows Vista 和 7 中的 UAC(用户帐户控制)和 UAC 虚拟化/数据重定向?您的文件可能实际上位于虚拟存储中。
用户帐户控制数据重定向
您的示例输出目录位于用户中,所以我不认为这会是问题,但这是一种值得一提的可能性,如果您不注意它,可能会非常令人沮丧!
希望这有帮助。
Have you read about UAC (User Account Control) and UAC Virtualization / Data Redirection in Windows Vista and 7? It's possible that your file is actually in the Virtual Store.
User Account Control Data Redirection
Your example output directory is in Users, so I wouldn't think this would be the issue, but it's a possibility worth mentioning and something that can be very frustrating if you're not looking out for it!
Hope this helps.
这段代码应该捕获任何错误。如果遇到任何错误,很可能是权限问题。确保您可以读取/写入要在其中创建文件的文件夹。
更新:
我刚刚在 Windows 7 Enterprise 下测试我的代码,第一次失败(设置了失败位)。然后我关闭用户帐户控制(UAC)并再次测试,它写入了该文件。这可能与您看到的问题相同。要关闭 UAC,请转至:
控制面板(通过小图标查看)|用户帐户 |更改用户帐户控制设置。将其设置为从不通知,然后单击确定按钮。您必须重新启动才能使更改生效。
我很好奇如何让它在 UAC 开启的情况下工作,我会研究一下。
This code should catch any error. Most likely it's a permissions thing if any errors are encountered. Make sure you can read/write to the folder you're creating the file in.
Update:
I just test my code under Windows 7 Enterprize and it failed the first time (fail bit was set). Then I turn off User Account Control (UAC) and tested again and it wrote the file. That is probably the same problem you're seeing. To turn off UAC go to:
Control Panel (view by Small icons) | User Accounts | Change User Account Control settings. Set it to Never notify then click OK button. You will have to restart for the changes to take affect.
I'm curious how to make it work with UAC on, i'll look into that.
试试这个:
Try this:
使用 FileMon 并查找进程中失败的 WriteFile 调用。
Use FileMon and look for failed WriteFile calls from your process.