无法在 C++ 中写入文件

发布于 2024-08-07 07:20:05 字数 404 浏览 12 评论 0原文

我正在尝试最基本的事情......用 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 技术交流群。

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

发布评论

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

评论(6

俏︾媚 2024-08-14 07:20:05

您需要以写入模式打开文件:

myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt", ios::out);

请务必查看第二个参数的其他选项。例如,如果您正在编写二进制数据,则需要 ios::binary

您还应该在打开流后检查它:

myfile.open(...
if (myfile.is_open())
    ...

更新:

AraK 是对的,我忘记了 ofstream 默认情况下处于写入模式,所以这不是问题。

也许您根本没有对该目录的写入/创建权限? Win7默认很多目录具有“拒绝全部”的特殊权限。或者该文件可能已经存在并且是只读的?

You need to open the file in write mode:

myfile.open ("C:\\Users\\Thorgeir\\Documents\\test.txt", ios::out);

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:

myfile.open(...
if (myfile.is_open())
    ...

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?

冷情妓 2024-08-14 07:20:05

首先扭转斜线。
即使 Windows 也能理解斜杠的反方向。

ofstream myfile("C:/Users/Thorgeir/Documents/test.txt");

您可以测试是否有任何错误:

if (!myfile)
{
    std::cout << "Somthing failed while opening the file\n";
}
else
{
    myfile << "Writing this to a file.\n";
    myfile.close();
}
  • 确保该目录存在。
  • 如果文件存在,请确保它是可写的(由您)
  • 检查您正在写入的目录是可写的(由您)

Start off by turning that slash around.
Even Windows understands the slash being the other way around.

ofstream myfile("C:/Users/Thorgeir/Documents/test.txt");

You could test if there are any errors:

if (!myfile)
{
    std::cout << "Somthing failed while opening the file\n";
}
else
{
    myfile << "Writing this to a file.\n";
    myfile.close();
}
  • Make sure the directory exists.
  • If the file exists make sure it is writeable (by you)
  • Check the directory you are writing into is writeable (by you)
帅气尐潴 2024-08-14 07:20:05

您是否了解过 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.

残月升风 2024-08-14 07:20:05

这段代码应该捕获任何错误。如果遇到任何错误,很可能是权限问题。确保您可以读取/写入要在其中创建文件的文件夹。

#include "stdafx.h"
#include <fstream>
#include <iostream>

bool CheckStreamErrorBits(const std::ofstream& ofile);

int _tmain(int argc, _TCHAR* argv[]) {
 std::ofstream ofile("c:\\test.txt");
 if(ofile.is_open()) {
  CheckStreamErrorBits(ofile);  
  ofile << "this is a test" << std::endl;
  if(CheckStreamErrorBits(ofile)) {
   std::cout << "successfully wrote file" << std::endl;
  }
 }else {
  CheckStreamErrorBits(ofile);
  std::cerr << "failed to open file" << std::endl;
 }

 ofile.close();
 return 0;
}

//return true if stream is ok.  return false if stream has error.
bool CheckStreamErrorBits(const std::ofstream& ofile) {
 bool bError=false;
 if(ofile.bad()) {
  std::cerr << "error in file stream, the bad bit is set" << std::endl;
  bError=true;
 }else if(ofile.fail()) {
  std::cerr << "error in file stream, the fail bit is set" << std::endl;
  bError=true;
 }else if(ofile.eof()) {
  std::cerr << "error in file stream, the eof bit is set" << std::endl;
  bError=true;
 }
 return !bError;
}

更新:
我刚刚在 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.

#include "stdafx.h"
#include <fstream>
#include <iostream>

bool CheckStreamErrorBits(const std::ofstream& ofile);

int _tmain(int argc, _TCHAR* argv[]) {
 std::ofstream ofile("c:\\test.txt");
 if(ofile.is_open()) {
  CheckStreamErrorBits(ofile);  
  ofile << "this is a test" << std::endl;
  if(CheckStreamErrorBits(ofile)) {
   std::cout << "successfully wrote file" << std::endl;
  }
 }else {
  CheckStreamErrorBits(ofile);
  std::cerr << "failed to open file" << std::endl;
 }

 ofile.close();
 return 0;
}

//return true if stream is ok.  return false if stream has error.
bool CheckStreamErrorBits(const std::ofstream& ofile) {
 bool bError=false;
 if(ofile.bad()) {
  std::cerr << "error in file stream, the bad bit is set" << std::endl;
  bError=true;
 }else if(ofile.fail()) {
  std::cerr << "error in file stream, the fail bit is set" << std::endl;
  bError=true;
 }else if(ofile.eof()) {
  std::cerr << "error in file stream, the eof bit is set" << std::endl;
  bError=true;
 }
 return !bError;
}

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.

无所的.畏惧 2024-08-14 07:20:05

试试这个:

if( ! myfile)
{
cerr << "You have failed to open the file\n";

//find the error code and look up what it means.
}

Try this:

if( ! myfile)
{
cerr << "You have failed to open the file\n";

//find the error code and look up what it means.
}
鹤舞 2024-08-14 07:20:05

使用 FileMon 并查找进程中失败的 WriteFile 调用。

Use FileMon and look for failed WriteFile calls from your process.

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