0

So I'm new to programming and I'm following a tutorial and I got to fstream, but I don't know if my compiler is acting weirdly or I'm missing a file or something but the .open function does not seem to work and fstream is acting weirdly. (Like you cannot use (ostreamobject)("test.txt"); I'm new to programming so please don't use technical terms.

I've searched around a bit but I didn't find anything.

I don't know what's wrong with my code or my compiler but outputFile.open does not exist weirdly enough. I'm using visual studio 2015. This is a small amount of code I wrote and it still comes with an error. Here's the code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
ostream oFile;
istream iFile;

oFile.open("test.txt");

    return 0;
}

Here's the error codes:

1>d:\dokument\visual studio 2015\projects\fstream\fstream\fstream.cpp(13): error C2512: 'std::basic_ostream>': no appropriate default constructor available 1> d:\programmering\vc\include\iosfwd(679): note: see declaration of 'std::basic_ostream>' 1>d:\dokument\visual studio 2015\projects\fstream\fstream\fstream.cpp(14): error C2512: 'std::basic_istream>': no appropriate default constructor available 1> d:\programmering\vc\include\iosfwd(678): note: see declaration of 'std::basic_istream>' 1>d:\dokument\visual studio 2015\projects\fstream\fstream\fstream.cpp(16): error C2039: 'open': is not a member of 'std::basic_ostream>' 1> d:\programmering\vc\include\iosfwd(679): note: see declaration of 'std::basic_ostream>' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Albzter
  • 13
  • 6

3 Answers3

2

The problem is, that you are using "ostream" and "istream" instead of "ofstream" and "ifstream" (note the "f" for "file").

Use this version:

#include "stdafx.h"
// #include <iostream> // you don't need this and it caused most of your confusion!
#include <fstream>
#include <string>

using namespace std;

int main()
{
   ofstream oFile;
   ifstream iFile;

   oFile.open("test.txt");

   return 0;
}

FYI: "ofstream" and "ifstream" are both superclasses of "ostream" and "istream". They provide further functions (like "open") for interacting with files

S.H
  • 875
  • 2
  • 11
  • 27
1

Well, it doesn't exist. There is no ostream constructor that takes a filename.

You meant ofstream.

You could have checked this out by simply visiting the documentation.

If your tutorial really says ostream, tell us what it is and stop using it.
You should learn C++ from a good book, not from random "tuts" on the internet.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

Consider declaring oFile and iFile as concrete files.

ofstream oFile;
ifstream iFile;
Franck
  • 1,635
  • 1
  • 8
  • 11