I'm trying to write a simple file output program for my class. The error I recieve when I try to compile in VS .net 2003 is the following.

fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory

The code to my program is extremely straight forward as I always test a concept before incorporating it into the final solution. It's as follows:

#include <math.h>
#include <fstream.h>
#include <iomanip.h>

void main()
{
	int a[25];
	double s[25];

	for(int n=0; n < 25; n++)
		s[n] = sin(3.14159265*(a[n]=n*15)/180);

	ofstream out("SineDataV2.txt", ios::out);

	for(n=0;n<25;n++)
		out << setw(5) << a[n] <<''<<setw(12)<<s[n]<<'\n';

	out.close();
}

Any ideas why I can't include it? I'm pretty sure I'm including the correct file as MSDN reference tells me so: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang98/HTML/_iostream_ofstream.asp

Thanks in advance to those that help!

Recommended Answers

All 4 Replies

>Any ideas why I can't include it?
Yes, fstream.h is not a standard C++ header. Nor is iomanip.h. Come to think of it, void main isn't standard either (regardless of what your compiler's documentation says). If you want to go fully standard, use C headers with the .h dropped and prefix them with C, and use C++ headers with the .h dropped. Then prefix every standard name with std:: (or you can use using namespace std if you want) because all standard names are in the std namespace. Also, there's no need to manually close the file. ofstream's destructor will handle that for you. Lastly, you had a bug where the first loop declares a variable yet you try to use it after the loop. This is wrong because the variable is declared within the scope of the loop and when the loop ends the variable is destroyed. You can do what you were doing with older versions of Visual C++, but not anymore:

#include <cmath>
#include <fstream>
#include <iomanip>

int main()
{
  int a[25];
  double s[25];

  for(int n=0; n < 25; n++)
    s[n] = std::sin(3.14159265*(a[n]=n*15)/180);

  std::ofstream out("SineDataV2.txt", std::ios::out);

  for(int n=0;n<25;n++)
    out << std::setw(5) << a[n] <<' '<<std::setw(12)<<s[n]<<'\n';
}

Greetings,

» Any ideas why I can't include it?
It sometimes depends. fstream is part of the C++ standard, fstream.h isn't.

The difference between fstream and fstream.h
fstream is somewhat more restrictive than the older fstream.h. One would possibly avoid problems by careful use of namespaces, but it would be a lot smarter to stick with one or the other.

You should avoid using the *.h version as much as possible, because some implementations have bugs in their *.h version. Moreover, some of them support non-standard code that is not portable, and fail to support some of the standard code of the STL.

Furthermore, the *.h version puts everything in the global namespace. The extension-less version is more stable and it's more portable. It also places everything in the std namespace.

Conclusion
fstream.h is an old style method of including std C++ headers, and in opposite to fstream you don't have to declare that you're using the std namespace.

It is sometimes recommended to use:

#include <fstream>
using namespace std;

Hope this helps,
- Stack Overflow

Thanks Narue and Stack Overflow for your quick and accurate replies!!

All is now good and I can get on with the rest of the lab :)

Do not forget to declare your fstream variables also. Like in <iostream> cout and cin are predeclared, in <fstream> you must declare these in advance to using them.

commented: Don't bump old threads -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.