Conflict with <fstream> and <vector>

M

Macca

Hi,

I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to
text files. I have now started to use vectors and when i added

#include <vector>
using namespace std;

to stdafx.h, I found that to compile i had to change <fstream.h> to
<fstream> to get it to compile.

When i run my program i have found that when i do something like:

CString filename,test;
test = "This is a test";
ofstream opdfs;
filename = "C:\\Filepath\\test.txt";

opdfs.open(filename, ios::eek:ut|ios::app);
opdfs << test <<endl;
opdfs.close();

It does not write the string to the file but a hex representation
0087385C.

Can anyone tell me why this is happening and how i can solve it?

Thanks In Advance
Macca
 
J

John Harrison

Macca said:
Hi,

I have been using <fstream.h> in stdafx.h,(i'm using MFC) to output to
text files. I have now started to use vectors and when i added

#include <vector>
using namespace std;

to stdafx.h, I found that to compile i had to change <fstream.h> to
<fstream> to get it to compile.

When i run my program i have found that when i do something like:

CString filename,test;
test = "This is a test";
ofstream opdfs;
filename = "C:\\Filepath\\test.txt";

opdfs.open(filename, ios::eek:ut|ios::app);
opdfs << test <<endl;
opdfs.close();

It does not write the string to the file but a hex representation
0087385C.

Can anyone tell me why this is happening and how i can solve it?

CString is a non-standard class, use std::string instead (header file
<string>)

I would imagine that some sort of invalid conversion to a pointer is
happening. Try being explicit

opdfs << (const char*)test <<endl;

but better still start using std::string, it is much better for nearly all
purposes than CString.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top