Newbie question : problem defining ofstream

problem defining ofstream in Win32 C++ program

Page 1 of 1

10 Replies - 1950 Views - Last Post: 02 June 2007 - 09:38 PM Rate Topic: -----

#1 bpinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-October 06

Newbie question : problem defining ofstream

Posted 10 February 2007 - 10:38 AM

Hi !

I'm creating a front end dialog based program in Windows XP with Visual C++ 2005. I'm trying to create an ofstream text file in one of the functions. I have this at the top of the code file :

[code]
using namespace system;

#include <fstream>
#include "stdafx.h"
#include "MPEG4FrontEnd.h"
#include "MPEG4FrontEndDlg.h"

[code]

and my code for creating the stream is :

[code]
void CMPEG4FrontEndDlg::CmdRemove()
{
BOOL islogfile;
CFile testlogfile;

int logmsgsize;

ofstream logfile; // here it is

CString logtext;
UINT copy_if_unpacked;
CString nyttnavn;

..more unimportant code
}
[code]

when I try to compile this, I get the following message at the ofstream creation :

d:\programmering\projects\mpeg4frontend\mpeg4frontend\mpeg4frontenddlg.cpp(427) : error C2065: 'ofstream' : undeclared identifier

I checked that the include should only be <fstream> (there is no fstream.h file, only an fstream file without an .h ending) and I've tried creation other streams than the ofstream as well, with the same result. What am I doing wrong here ? Some kind of setting perhaps ?

Is This A Good Question/Topic? 0
  • +

Replies To: Newbie question : problem defining ofstream

#2 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 10:48 AM

add using std::ofstream; to the top of your file

This post has been edited by realNoName: 10 February 2007 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 10:50 AM

try
using namespace std;


Was This Post Helpful? 0
  • +
  • -

#4 bpinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-October 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 11:06 AM

View PostrealNoName, on 10 Feb, 2007 - 10:48 AM, said:

add using std::ofstream; to the top of your file


Thanks for replying. The top of my file now looks like this :

// MPEG4FrontEndDlg.cpp : implementation file

//
using std::ofstream;
using namespace system;

#include <fstream>
#include "stdafx.h"
#include "MPEG4FrontEnd.h"
#include "MPEG4FrontEndDlg.h"

[code]

and the declaration of ofstream has not changed :

[code]

void CMPEG4FrontEndDlg::CmdRemove()
{
	BOOL islogfile;
	CFile testlogfile;
	
	int logmsgsize;

	ofstream logfile;  // here it is
	
	CString logtext;
	UINT copy_if_unpacked;
	CString nyttnavn;

...more uniportant code

[code]

still get the followin message :

error C2065: 'ofstream' : undeclared identifier

Am I using the tip wrong or is there another solution to this ?


[quote name='horace' post='201467' date='10 Feb, 2007 - 10:50 AM']
try
[code]
using namespace std;


[/quote]

Didn't work that either
Was This Post Helpful? 0
  • +
  • -

#5 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 12:08 PM

sorry you need to put it under your #include<..>

so it should look like this
#include <fstream>
#include "stdafx.h"
#include "MPEG4FrontEnd.h"
#include "MPEG4FrontEndDlg.h"
using std::ofstream;


and you dont need to include both std::ofstream and namespace::std just one of them

This post has been edited by realNoName: 10 February 2007 - 12:14 PM

Was This Post Helpful? 0
  • +
  • -

#6 bpinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-October 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 12:15 PM

View PostrealNoName, on 10 Feb, 2007 - 12:08 PM, said:

sorry you need to put it under your #include<..>

so it should look like this
#include <fstream>
#include "stdafx.h"
#include "MPEG4FrontEnd.h"
#include "MPEG4FrontEndDlg.h"
using std::ofstream;


and you dont need to include both std::ofstream and namespace::std

namespace::std includes most of the common things like cout,cin,endl,ect...
std::ofstream just includes the out stream


My code in the beginning now looks like this :

[code]

#include <fstream>
#include "stdafx.h"
#include "MPEG4FrontEnd.h"
#include "MPEG4FrontEndDlg.h"
using std::ofstream;

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

[code]

and I now get the message :

error C2039: 'ofstream' : is not a member of 'std'

and it "points" to the "using std::ofstream" line
Was This Post Helpful? 0
  • +
  • -

#7 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 12:20 PM

i might be thinking of the wrong file... did you try the namespace std b/c that one should work

This post has been edited by realNoName: 10 February 2007 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#8 bpinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-October 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 12:24 PM

View PostrealNoName, on 10 Feb, 2007 - 12:20 PM, said:

i might be thinking of the wrong file... did you try the namespace std b/c that one should work


Tried using namespace std; both over and under the include file, but I the just get the usual "error C2065: 'ofstream' : undeclared identifier' at the line where I try to declare an ofstream
Was This Post Helpful? 0
  • +
  • -

#9 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 01:26 PM

Do you set logfile to anything some where... i just tried to do my own file and it works for me so my only other guess is that you did not open the file anywhere

this is what i did to test

Quote

#include <fstream>
using std::ofstream;

int main()
{
ofstream logfile;
logfile.open("test.txt");
logfile << "Just a test";
logfile.close();
return 0;
}

This post has been edited by realNoName: 10 February 2007 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#10 bpinn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 11-October 06

Re: Newbie question : problem defining ofstream

Posted 10 February 2007 - 01:32 PM

View PostrealNoName, on 10 Feb, 2007 - 01:26 PM, said:

sorry man i dont know whats going on... i just tried to do my own file and it works for me so there might be a problem some where else in your code


this is what i did to test

Quote

#include <fstream>
using std::ofstream;

int main()
{
ofstream logfile;
logfile.open("test.txt");
logfile << "Just a test";
logfile.close();
return 0;
}


Thanx for trying to help. As I said before, I'm using MFC in WinXP to create this program (a dialogbased windows program), so there might be som settings or something else I don't know about that does this. The test you did are in standard C/C++ programming, so that may be the reason why it doesn't work in my program. Anyway, thanks again for trying.
Was This Post Helpful? 0
  • +
  • -

#11 matssundin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-June 07

Re: Newbie question : problem defining ofstream

Posted 02 June 2007 - 09:38 PM

Try turning off precompiled headers in the c/c++ property settings for the project.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1