简体   繁体   English

无法用C ++编写文件

[英]Unable to write file in C++

I'm trying to to the most basic of things .... write a file in C++, but the file is not being written. 我正在尝试最基本的东西....用C ++编写一个文件,但文件没有写入。 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. 我正在运行Windows 7 64位,如果这与此有关。 It's like file-write operations are completely forbidden and no error messages or exceptions are shown. 这就像文件写入操作是完全禁止的,并且不会显示错误消息或异常。

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. 如果您正在编写二进制数据,则需要ios::binary

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. AraK是对的,我忘了默认情况下ofstream处于写入模式,所以这不是问题。

Perhaps you simply don't have write/create permissions to the directory? 也许您根本没有对目录的写入/创建权限? Win7 defaults a lot of directories with special permissions of "deny all". Win7默认许多具有“拒绝所有”特殊权限的目录。 Or perhaps that file already exists and is read-only? 或者该文件可能已存在并且是只读的?

Start off by turning that slash around. 通过转动斜线开始。
Even Windows understands the slash being the other way around. 即便是Windows也能理解斜线是另一种方式。

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) 检查您写入的目录是否可写(由您)

Have you read about UAC (User Account Control) and UAC Virtualization / Data Redirection in Windows Vista and 7? 您是否了解过Windows Vista和7中的UAC(用户帐户控制)和UAC虚拟化/数据重定向? 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. 希望这可以帮助。

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). 更新:我只是在Windows 7 Enterprize下测试我的代码,它第一次失败(设置了失败位)。 Then I turn off User Account Control (UAC) and tested again and it wrote the file. 然后我关闭用户帐户控制(UAC)并再次测试并写入文件。 That is probably the same problem you're seeing. 这可能与您所看到的问题相同。 To turn off UAC go to: 要关闭UAC,请转到:

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. 我很好奇如何让它与UAC一起工作,我会调查一下。

Try this: 尝试这个:

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

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

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

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

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