Solved

i have problem in c++ ofstream ?

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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