|
-
Jan 22nd, 2003, 10:51 PM
#1
Unicode To ANSI FORMAT
Hi Everyone,
i have been assigned a task. i have to convert a 100 unicode files into ascii text files.
i have found out a method of identifying whether a file is unicode or not.
This method is working for all the unicode files. However I am not certain how do I automate the task for conversion. I have noticed that notepad allows me to save the files as text. This is not the best way to go about it as it involves opening the file and saving it again.
Does anyone know how do you convert a file from unicode to text? What does notepad do ? Can I use the same API call without bothering to open a file?
How do I do this in C++?
Cheers!
Abhijit
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jan 22nd, 2003, 11:08 PM
#2
Hyperactive Member
I think WideCharToMultiByte() or MultiByteToWideChar() make those conversions.
-
Jan 22nd, 2003, 11:18 PM
#3
Could you please do me a favor and post some code?
Cheers!
Abhijit
Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
save a blobFileStreamDataTable To Text Filemy blog
-
Jan 23rd, 2003, 12:54 AM
#4
Hyperactive Member
Sorry, I don't know how to use these functions, but everything about them is here:
http://msdn.microsoft.com/library/de...icode_17si.asp
-
Jan 23rd, 2003, 05:18 AM
#5
I needed something to relax.
This function returns TRUE if and only if the file was converted from UNICODE to ANSI. If it already was ANSI or if any error occurred (memory or IO) it returns FALSE.
If the error occurred when writing the final result back to the file the original file is GONE! You might want to change the function so that it has a target file different from the source.
Code:
// Assumes no file is larger than 0x7fffffff bytes.
BOOL ConvertFileToAnsi(LPCTSTR szFileName, BOOL bForce)
{
HANDLE hFile = CreateFile(szFileName, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_FLAG_RANDOM_ACCESS, NULL);
if(hFile == INVALID_HANDLE_VALUE)
return FALSE;
DWORD dwSize;
dwSize = GetFileSize(hFile, NULL);
if(liSize.LowPart == INVALID_FILE_SIZE)
{
CloseHandle(hFile);
return FALSE;
}
WCHAR *pwcBuffer = NULL;
pwcBuffer = (WCHAR *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize+sizeof(WCHAR));
if(pwcBuffer == NULL)
{
CloseHandle(hFile);
return FALSE;
}
DWORD dwRead;
BOOL bRet = ReadFile(hFile, pwcBuffer, dwSize, &dwRead, NULL);
if(!bRet)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pwcBuffer);
return NULL;
}
bRet = IsTextUnicode(pwcBuffer, liSize.LowPart, NULL);
if(bRet)
{
int iNeed = WideCharToMultiByte(CP_ACP, 0, pwcBuffer, -1,
NULL, 0, NULL, NULL);
char *pcConverted = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, iNeed+1);
if(pcConverted == NULL)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pwcBuffer);
return FALSE;
}
iNeed = WideCharToMultiByte(CP_ACP, 0, pwcBuffer, -1, pcConverted, iNeed,
NULL, NULL);
HeapFree(GetProcessHeap(), pwcBuffer);
if(iNeed == 0)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pcConverted);
return FALSE;
}
dwRead = SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
if(dwRead == INVALID_SET_FILE_POINTER)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pcConverted);
return FALSE;
}
bRet = SetEndOfFile(hFile);
if(!bRet)
{
CloseHandle(hFile);
HeapFree(GetProcessHeap(), pcConverted);
return FALSE;
}
bRet = WriteFile(hFile, pcConverted, iNeed, &dwRead, NULL);
HeapFree(GetProcessHeap(), pcConverted);
}
CloseHandle(hFile);
return bRet;
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|