由于不可能从一个程序中调用 2 个控制台,因此我在 game.exe 中编写了将日志保存到文件中,并且 log.exe 应该读取日志中的文本并将其显示在屏幕上。在 game.exe 中一切正常。问题在于阅读。那些。log.exe 不会在屏幕上显示任何内容。
正如您在屏幕截图中看到的那样,发生了变化(我按了 D)。
“RIGHT CALLED”已写入文件。但是控制台中什么都没有。日志.cpp:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <Windows.h>
#include <ostream>
#include <fstream>
using namespace std;
int main()
{
bool exit_=0;
string x, previousX;
ifstream bob("log.txt");
while (!exit_) {
bob >> x;
if (x == "EXIT CALLED") { exit_ = 1; }
if (x == previousX) { continue; }
cout << x;
previousX = x;
Sleep(50);
}
return 0;
}
最有可能的问题是文件处于启动时的状态
log.exe。尝试将文件的开头移动到循环的主体,记住在最后关闭文件。然后一切都应该工作。