Solved

i have problem in c++ ofstream ?

Posted on 2010-08-17
3
338 Views
Last Modified: 2012-05-10
hello,

my problem appear after i changed the character set to Unicode from project properties

the function i attached here
void addToFile(CString data,char* file)
{
	if(data == " "){}else{		
   ofstream log(file,ios::app);		
      log << data;
      log <<'\0';
      log.close();
	}
}

Open in new window

after excute it i found in the file only the address of data not the data


can any body fix that


thank you
 
0
Comment
Question by:sa3q
3 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33461149
Use wofstream. CString data is the wide string now.
In MFC there is CFile class. If you use CString, you can use CFile too.

CodeProject. Upgrading an STL-based application to use Unicode.
http://www.codeproject.com/KB/stl/upgradingstlappstounicode.aspx
0
 
LVL 5

Expert Comment

by:xdomains
ID: 33461406
You could use it as below -

log << data.GetString();

This will return you the right string depending on your build settings (unicode/multibyte)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33462574
Use basic_ofstream to create the appropriate stream type depending on whether the project is Unicode or MBCS.
void addToFile(CString data,char* file)

{

      if(data == " "){}else{          

      basic_ofstream<TCHAR> log(file,ios::app); // <-- will always be correctly wide or narrow     

      log << data;

      log <<'\0';

      log.close();

        }

}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now