[英]Difficulties with the handling of files after shutdown or reboot
I wrote a little application which is able to log some data and writes it via ofstream into a file: 我编写了一个小应用程序,它能够记录一些数据并将其通过ofstream写入文件中:
ofstream filename;
filename.open("filename.ext", ios::out | ios::app);
filename << stuff;
This file is closed and re-opened every 30sec. 该文件每30秒关闭并重新打开一次。 A second file "settings.ext" is used to define the programms behavior.
第二个文件“ settings.ext”用于定义程序行为。 The function looks like following:
该函数如下所示:
ifstream input("settings.qib", ios::in);
string number;
for (string line; getline(input, line); )
{
if (line.find("updateInterval") != string::npos) {
number = "";
for (int i = 0; !isspace(line.at(i)); i++) number += line.at(i);
updateInterval = stoi(number);
}
if (line.find("visibility") != string::npos) {
number = "";
for (int i = 0; !isspace(line.at(i)); i++) number += line.at(i);
Stealth(stoi(number));
}
if (line.find("console") != string::npos) {
number = "";
for (int i = 0; !isspace(line.at(i)); i++) number += line.at(i);
console = stoi(number);
}
}
This was the best i could come up with to read out a file like so: 这是我想出的最好的方法来读取这样的文件:
0 = visibility
0 =可见度
1 = console
1 =控制台
30 = updateInterval
30 = updateInterval
Everything works great. 一切正常。 The settings are refreshed in the same rythm as the log file is flushed.
刷新设置与刷新日志文件的节奏相同。
I created a Registry Entry for the entire to program to startup at every start, which works fine aswell. 我为整个程序创建了一个注册表项,以便在每次启动时进行编程以使其正常运行。 But after a shutdown or reboot the program seems to ignore the two files.
但是在关闭或重新启动后,程序似乎忽略了这两个文件。 No new content is added to the log file and the settings arent refreshd either.
没有新内容添加到日志文件,并且设置也没有刷新。
I am guessing this has something to do with the fact, that log file is open during the shutdown, but this doesnt explain why the setting file isnt touched either. 我猜想这与以下事实有关:在关闭过程中打开了日志文件,但这不能解释为什么设置文件没有被触及。
I have read some things about the WM_ENDSESSION
but I'm not sure if that is the right way to go. 我已经阅读了有关
WM_ENDSESSION
一些内容,但是我不确定这是否是正确的方法。
I'm open for all kinds of answers, or other ways to achieve my goal. 我愿意接受各种答案,也可以通过其他方式实现自己的目标。
After many reboots i figured, that current working directory is stated as /system32 after the startup. 我发现许多重新启动后,启动后该当前工作目录被表示为/ system32。 No wonder the files could be opened.
难怪文件可以打开。 First: In system32 are no such files located.
第一:在system32中找不到此类文件。 Second: The program doesnt have permission to write in that directory.
第二:程序无权在该目录中写入。 To solve this, I changend the directory using _chdir() and everythink works like a charm.
为了解决这个问题,我使用_chdir()更改了目录,everythink就像一个超级按钮一样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.