即使在包含 std 命名空间之后,fstream 在 Visual Studio 2017 中也无法正常工作

fstream is not working in Visual Studio 2017 even after including the std namespace

#include <iostream>
#include <fstream>
#include "stdafx.h"
using namespace std;
int main() 
{
    ofstream myfile;
    myfile.open("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
}

我正在尝试使用 visual studio 2017 在 C++ 中进行基本文件处理,似乎无法使用 class fstream

error - Severity Code Description Project File Line Suppression State Error C2065 'myfile': undeclared identifier CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 11
Error C2065 'ofstream': undeclared identifier CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 8
Error C2146 syntax error: missing ';' before identifier 'myfile' CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 8
Error C2065 'myfile': undeclared identifier CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 8
Error C2065 'myfile': undeclared identifier CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 9
Error C2228 left of '.open' must have class/struct/union CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 9
Error C2065 'myfile': undeclared identifier CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 10
Error C2228 left of '.close' must have class/struct/union CSV C:\Users\User\source\repos\CSV\CSV\CSV.cpp 11

我该怎么办?

编译器忽略 #include "stdafx.h" 之前的所有内容,因此将该行移至其他包含指令上方。