Problem with #include <ifstream>

Aug 28, 2019 at 4:52pm
I have the following lines in my program:

1
2
3
#include <fstream>
#include <ifstream>
#include <ofstream> 


After ‘building’, Visual Studio says about line 2: "Cannot open source file ifstream", and "Cannot open include file: 'ifstream': No such file or directory"

There are similar messages about line 3.

Please can someone tell what is wrong with these two lines?
Aug 28, 2019 at 4:54pm
Those lines are fine. Something's wrong with the configuration of your project. Are you perchance building your project as a C project by mistake?

-Albatross


EDIT: Got my wires crossed. See below.
Last edited on Aug 28, 2019 at 8:19pm
Aug 28, 2019 at 4:58pm
Both ifstream and ofstream are defined in the fstream header you shouldn't need anything other than #include <fstream>
Aug 28, 2019 at 4:58pm
Avoiding assumptions ... you don't happen to be on a very, very old version of VS do you?
Aug 28, 2019 at 8:06pm
The header files ifstream and ofstream are not in the C++ standard.
As jlb said, it's just <fstream> which contains all the classes.

https://en.cppreference.com/w/cpp/header

___________________________________________

Edit: It's worth asking, where did you find the code? On a random comment in an MSDN forum post? Or an actual codebase?

It seems there did used to be pre-standard headers in compilers a long time ago, like g++, where <ifstream.h> was a thing, but I can't find anything specifically about <ifstream>. So jonnin might also be correct, you might be looking at source code used with a gosh darn old compiler.
Last edited on Aug 28, 2019 at 8:17pm
Aug 29, 2019 at 7:48am
Many thanks for these points.

I have found references to ifstream and ofstream in at least one book and examples on the web.

I'm using the 'community' version of Visual Studio which has been downloaded and installed quite recently but compared with other versions may be "very very old".
Aug 29, 2019 at 12:36pm
Just to be clear: There's nothing wrong with your compiler/Visual Studio, it's working as intended.
It's the book or web examples that are wrong.
Last edited on Aug 29, 2019 at 12:36pm
Aug 29, 2019 at 1:40pm
compared with other versions may be "very very old".
I was talking about pre year 2000, back before the no .h header file syntax.
the community edition is just fine.

post a small example of what you are doing that isnt working and let us see if we can spot the issues if you still need help.
Last edited on Aug 29, 2019 at 1:48pm
Topic archived. No new replies allowed.