Solved

i have problem in c++ ofstream ?

Posted on 2010-08-17
3
344 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 39
Exception thrown at 0x00007FFD5BC81F28 7 44
What is sub-make ? 2 60
max float value 3 41
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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