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

左心房为你撑大大i 提交于 2019-12-24 02:28:07

问题


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 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?


回答1:


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.



来源:https://stackoverflow.com/questions/5477573/why-doesnt-creating-writing-to-file-with-fstream-work-on-windows-start-up

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!