|
Here's one way:
wstring content = L"Test String";
ofstream fout;
fout.open("e:\\testutf8.txt", ios_base::out | ios_base::trunc | ios_base::binary);
fout.write("\xEF\xBB\xBF", 3);
int bufsize = ::WideCharToMultiByte(CP_UTF8, 0, content.c_str(), -1, NULL, 0, NULL, NULL);
LPSTR buf = new CHAR[bufsize];
bufsize = ::WideCharToMultiByte(CP_UTF8, 0, content.c_str(), -1, buf, bufsize, NULL, NULL);
fout.write(buf, bufsize-1);
fout << flush;
fout.close();
delete[] buf;
That should match what Notepad writes.
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hello Mark,
Thank you very much it works fine.
I did many ways but all were in vain. Main reason was because I convinced my self that it has necessarily to be done
through an wofstreamù instead of ofstream in order to handle the std::wstring content..
many thanks indeed.
|
|
|
|
|
hINTModuleState wrote: through an wofstreamù instead of ofstream in order to handle the std::wstring content..
There's probably a way to do the conversion to UTF-8 using the wcstombs()
function but I just went with what I know...
just go straight to the conversion API and write the result as a binary byte stream
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I have a simple server app that uses WritePrivateProfileString() when a client changes the status of a user. I want to protect against multiple 'change status' requests resulting in clashing file writes.
The clients communicate with the server using UDP and the server uses a listener socket. The listener socket uses 'OnReceive()' to call a function to write to the data file.
I've been using a temp file to write to and then delete the working file then rename the temp file. It works most of the time but once in a while the main data file is deleted and there is no temp file. It's a bug but I'm wondering if there is a better way?
Thank you.
|
|
|
|
|
Does each client get serviced on a separate thread by the server? If yes, you'll need to use some kind of synchronization object while writing to the file. A CRITICAL_SECTION might do nicely for you.
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG
|
|
|
|
|
I'm using a single instance of the listener socket. Would that be enough to ensure the file is only written by one client at a time?
|
|
|
|
|
What do you do when your "accept" (or the equivalent Windows socket extensions API) call returns? Do you launch another thread to respond to the client socket returned by "accept"? If you are doing it on the same thread as the one that invokes "accept" then you shouldn't require synchronization; in which case the problem lies elsewhere.
--
gleat
http://blogorama.nerdworks.in[ ^]
-- Number Two's eyes narrowed and became what are known in the Shouting and Killing People trade as cold slits, the idea presumably being to give your opponent the impression that you have lost your glasses or are having difficulty keeping awake. Why this is frightening is an, as yet, unresolved problem. -- HHGTG
|
|
|
|
|
I have a pointer to the server object (the object that created the listener socket), and I call a function in that server object. No additional threads.
Thank you for your time on this.
|
|
|
|
|
OK. You might want to conduct a little experiment. From your "OnReceive" handler, trace the return value of "GetCurrentThreadId" to a log and have multiple clients connect simultaneously to the server. Now inspect the log to see if the values logged are the same for all the clients.
Also, is there a code snippet that you can post that shows how you are doing the file processing?
|
|
|
|
|
CORBA::Boolean CScope::Fineroot
(
IlAdapter::BaseTable*& corbaTexts,
{
corbaTexts = new IlAdapter::BaseTable;
if (result)
{
}
return result;
}
modified on Monday, January 12, 2009 8:53 AM
|
|
|
|
|
Please read Chris's "how to get someone to care enough to answer your question" rules.
One of them mentions the pre tag.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Giving a real answer...
We can have no idea when to delete the memory you just new ed. You're showing a member function that makes a new object for you.
The answer to your question depends on the code that calls this function. We have no idea about that.
result is not defined, and not shown where it comes from either.
I know we're clever in this forum, but we're not gods. (OK, CPallini might be)
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Iain Clarke wrote: OK, CPallini might be
Always mocking that poor guy!
BTW: Do you know my new site, www.free.techie.god.answerz.com?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Me think it should be at least in the caller of the method CScope::Fineroot .
It depends where the data will used and how low should it life should be.
|
|
|
|
|
ptr_Electron wrote: CORBA
Ok, there is the problem . No seriously, doing memory management in CORBA is really a pain. If you don't even understand the basic concepts of memory management in C++, then you'll be in great troubles. If you really wants to understand things a bit more in details, you really have to read a book on CORBA and understand it, otherwise you'll be completely lost. And almost nobody here will be able to help you because it's too specific to your application.
Good luck...
|
|
|
|
|
If you are not going to use it outside the Fineroot function, then delete it at the end.
The actual confusion is that you are not showing us what you are gonna do with corbaTexts. If you're going to use it outside the scope of this function, delete it at the place where it will be used (and *after* it is used).
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Hi.....
I have a small problem in getting the hostname of the mapped drive.
I have mapped a shared folder of a machine in network on my system.
I have to get the name of the machine from the mapped drive.
I was not able to find any proper function for that.
Please do let me know if there is some function or some way to do it, because in my program i have to get the name of the machine.Is there a way out?
Please do help me out.
Thnx in advance...
|
|
|
|
|
raesa wrote: Please do let me know if there is some function or some way to do it, because in my program i have to get the name of the machine.Is there a way out?
Have you tried WNetGetConnection() ?
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
oops
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
|
I did some digging, and the following code worked for me:
TCHAR szBuf [MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dw = WNetGetConnection (_T("p:"), szBuf, &dwSize);
It gave me "\\myserveraddress\sharename" in szBuf.
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
|
That was one of the first results in google, but it's using .net namespace functionality, and mapping drives.
So almost the exact opposite of what he needs.
Phew, regained my sense of superiority after replying to the wrong person earlier!
Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
|
|
|
|
|
Iain Clarke wrote: Phew, regained my sense of superiority after replying to the wrong person earlier!
You can still delete that post and hardly anyone would know.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
|
|
|
|
|
Yeah I didnt see .net I delete previous post.
Now I changed title of my reply.
Of one Essence is the human race
thus has Creation put the base
One Limb impacted is sufficient
For all Others to feel the Mace
(Saadi )
|
|
|
|