0
#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;
}

I am trying out basic file handling in c++ using visual studio 2017, can't seem to use the 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

what should I do ?

1 Answers1

2

The compiler ignores everything before #include "stdafx.h" so move that line above the other include directives.

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70
  • Thanks it worked out... btw why is the stdafx.h file needed in visual studio ? – Pranav Lokhande Mar 03 '18 at 17:25
  • @PranavLokhande: This other answer should help: [What's the use for “stdafx.h” in Visual Studio?](https://stackoverflow.com/a/4726838/445976). Note that precompiled headers are *optional* and you can decide not to use them if you wish. – Blastfurnace Mar 03 '18 at 17:28