Code::Blocks build fails

Learning C++ although I did some self taught hobby C programming a long time ago.

First I started up Code::Blocks and chose "create a new project".
In the New from template window I double clicked "Console application" icon.
Next chose C++ option.
Next gave a Project title: "FileTest" and selected folder C:\CodeBlockPrograms.
Then hit the Finish button.
In the Management section on the left I chose the Files tab and navigated to the chosen FileTest folder.
Double clicked main.cpp file to edit it.
The template comes with an #include <iostream> but no #include <fstream>.
So I typed in #include <fstream.h>
Then chose Build -> Build and run
First I got a fatal error: fstream.h: No such file or directory.
When I changed the <fstream.h> to <fstream>it worked but why when the tutorial example used the former?

Then when I typed in the rest of the program and tried a build/run I got a fatal error with regards to the mode value.

Anyone know how to fix that?

||=== Build: Debug in FileTest (compiler: GNU GCC Compiler) ===|
C:\CodeBlockPrograms\FileTest\main.cpp||In function 'int main()':|
C:\CodeBlockPrograms\FileTest\main.cpp|10|error: invalid conversion from 'int' to 'std::ios_base::openmode {aka std::_Ios_Openmode}' [-fpermissive]|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|801|error: initializing argument 2 of 'std::basic_fstream<_CharT, _Traits>::basic_fstream(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]' [-fpermissive]|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
   char ch;
   char filename[20] = "a:test.txt";
   int mode = ios::out;
   fstream fout(filename, mode);
   cout << "Ready for input: Use Control-Z to end." << endl;
   while (cin.get(ch))
   {
       fout.put(ch);
   }
   fout.close();

    return 0;
}


The mode used to open an fstream is not an integer, and so you cannot assign it to one. You can declare mode to be of type std::ios_base::openmode, or use the auto keyword if you're using a recent compiler that supports it.
@Zhuge,
Thank you that seems to worked. My tutorial must be out of date.
If your tutorial recommend fstream.h, it likely is, as that isn't even standard C++.
Topic archived. No new replies allowed.