Connecting Tech Pros Worldwide Help | Site Map

Conflict with <fstream> and <vector>

Macca
Guest
 
Posts: n/a
#1: Jul 22 '05
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::out|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
John Harrison
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Conflict with <fstream> and <vector>



"Macca" <macca18112002@yahoo.co.uk> wrote in message
news:e33e0a9e.0410220255.113a4a2f@posting.google.c om...[color=blue]
> 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.[/color]

<fstream.h> is a non-standard header file. Don't use it.
[color=blue]
>
> 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::out|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?[/color]

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


Closed Thread