Confusion about File IO and Visual Studio 2015

Hey guys RPA here, with yet another question. So I was using Visual Studio 2015 (Community) to code a console app that uses File I/O. I was working fine with the headers iostream and string, but when I included fstream, the problems started. I wrote the following code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>

int main()
{
    std::cout << "Hello, World!\n";
    fstream outfile("data.txt",ios::app | ios::out);
    if(!outfile)
    {
        std::cout << "Unable to create file!";
    }
    else outfile << "Hello, World!";


After I wrote this down, I noticed the intellisense had a red underline under the word fstream. Upon inspection, it said, "identifier fstream is not defined". To change my code, I changed the header to ofstream, and the line:
 
fstream outfile("data.txt",ios::app | ios::out);

to
 
ofstream outfile("data.txt",ios::app);

, but then it said that the header ofstream was not found and ofstream was undefined. I even tried ifstream, but to no prevail.

Any thoughts/solution for my predicament?
It's std::fstream and std::ofstream
You don't have using namespace std; (a good thing), however by omitting that everything in the std namespace must be qualified.

Try this instead:
 
  std::fstream outfile (...);
closed account (E0p9LyTq)
Your problem is not specific to VS 2015 Community. Failing to specify the std:: namespace will cause errors with any current C++ compiler.
Topic archived. No new replies allowed.