【发布时间】:2015-06-09 10:17:25
【问题描述】:
我的程序目前正在做的是读取一个文本文件,然后在屏幕上显示内容。我想要它做什么,每次更改文件时读取文件,然后刷新屏幕并显示新文件内容。我试图实现一个循环,但在尝试运行时出现异常错误。这是我当前的循环设置:
void number(HWND hwnd){
HANDLE dwChangeHandles[2];
char path[250];
ExpandEnvironmentStrings("%AppData%\\\ECDisplay\\", path, 250);
char versionpath[250];
ExpandEnvironmentStrings("%AppData%\\\ECDisplay\\nursery.txt",
versionpath, 250);
std::ifstream file(versionpath);
std::string str;
while (std::getline(file, str)){
nurserynumber = str;
file.close();
}
dwChangeHandles[0] = FindFirstChangeNotification(
path, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_LAST_WRITE); // watch file name changes
if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
{
}
else{
number(hwnd);
}
}
编辑 我最初收到的错误: EC Display.exe 中 0x7789EA0B (ntdll.dll) 处未处理的异常:0xC0000374:堆已损坏(参数:0x778D4270)。
编辑 2 这是我当前的代码:
void number(HWND hwnd){
HANDLE dwChangeHandles[2];
char path[250];
ExpandEnvironmentStrings("%AppData%\\\ECDisplay\\", path, 250);
char versionpath[250];
ExpandEnvironmentStrings("%AppData%\\\ECDisplay\\nursery.txt",
versionpath, 250);
std::ifstream file(versionpath);
std::string str;
dwChangeHandles[0] = FindFirstChangeNotification(
path, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_LAST_WRITE); // watch file name changes
do{
while (std::getline(file, str)){
nurserynumber = str;
}
} while (dwChangeHandles[0] != INVALID_HANDLE_VALUE);
}
【问题讨论】:
-
没有足够的信息来帮助找到崩溃,但是在
while(getline)循环中关闭文件对你没有多大好处。 -
如果你阅读
FindFirstChangeNotificationdocumentation,你会发现你需要在返回的句柄上使用“等待函数”来获得任何类型的实用程序。
标签: c++