C++ 无法将数据输出到IF语句C++;

C++ 无法将数据输出到IF语句C++;,c++,visual-studio,file-io,fstream,ofstream,C++,Visual Studio,File Io,Fstream,Ofstream,我试图在if语句中的文本文件“data.txt”中输出距离和时间数据。我知道正在执行if语句中的操作,因为我可以在我的“即时窗口”(使用Visual Studio 2017)中看到“Hello!”和“distance:5 time:0”字样的打印,但我的文本文件Data.txt为空!我任意设置了distance=5和time=0,以使我的代码更易于调试 //opening data file to write to ofstream datafile("Data.txt"); if (Hand

我试图在
if
语句中的文本文件“data.txt”中输出距离和时间数据。我知道正在执行
if
语句中的操作,因为我可以在我的“即时窗口”(使用Visual Studio 2017)中看到“Hello!”和“distance:5 time:0”字样的打印,但我的文本文件Data.txt为空!我任意设置了distance=5和time=0,以使我的代码更易于调试

//opening data file to write to
ofstream datafile("Data.txt");

if (HandClosedOnTarget(rightHandState, rightHandPosition, targetPositions[currentTargetIndex]))
        {
        secondsPassed = 0;
        distance = 5;

        datafile << "time (sec): " << secondsPassed << " distance: " << distance << "\n";

        TRACE(L"Hello!\n");
        TRACE(L"distance = %s time: %s \n", std::to_wstring(distance).c_str(), std::to_wstring(secondsPassed).c_str());
        }
//正在打开要写入的数据文件
流数据文件(“Data.txt”);
if(HandClosedOnTarget(rightHandState、rightHandPosition、targetPositions[currentTargetIndex]))
{
secondsPassed=0;
距离=5;

数据文件你确定你看到的是正确的文件吗?默认目录是
Documents\visualstudio 2017\Projects\Project Name\Project Name
,这是你唯一面临的问题吗?还是你的程序崩溃得更厉害了?@NathanOliver我相信我看到的是正确的file@MikeNakis这是我唯一要面对的问题当你处理完你的文件并且在你的程序终止之前,你是否正确地关闭了它?