Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

i have problem in c++ ofstream ?

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 233
Find Visual Studio Tools 2 111
sorting efficency of sorting algorithm 30 121
show out valin of json for debugging in visaul c++ 1 41
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

840 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