开发者

Why doesn't creating/writing to file with fstream work on windows start up?

开发者 https://www.devze.com 2023-02-20 10:01 出处:网络
So here I got this little program which everyone obviously understands. include <iostream> include <fstream>

So here I got this little program which everyone obviously understands.

include <iostream>  
include <fstream>  
using namespace std;  

int main () {  

 ofstream myfile;  
  myfile.open ("example.txt");  
  myfile << "Writing this to a file.\n";  
  myfile.close();  
  return 0;  
}  

This program works just fine, however, the problem is that if I have added it to run on windows start 开发者_JAVA技巧up(in registries SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run), the programs starts fine, but it does not create example.txt file. Why is that and how do I avoid it?


It tries to create the file in the current working directory for the process, since you specified a relative path.

Because there is no way to specify the working directory in the Windows\CurrentVersion\Run registry setting, the working directory is inherited from the parent process. The parent process is the shell, explorer.exe which has a working directory of C:\Windows\system32. Since (assuming UAC is enabled) your user doesn't have rights to that folder, no file is created.

0

精彩评论

暂无评论...
验证码 换一张
取 消