Every Alternative Character is NULL while writing to a file
-
Hello Everybody, I am facing a strange problem. Requirement: Get the length of contents of a file and modify the header present in the file to represent the length in Hexadecimal format. Existing Code:
if( cFilePointer.Open(strFileName ,CFile::modeRead) )
{
strContents.Empty();
dwRead = 0;
// Read the contents of the file and store in the CString object.
do
{
dwRead = cFilePointer.Read(cBuffer,100);
if ( dwRead > 0)
strContents.operator +=(cBuffer);
}while(dwRead > 0 );// Retrieve the length of the string and convert to Hex decimal number int iTotalLength = strContents.GetLength(); sprintf(cLengthOfString,"%4x",iTotalLength); for ( int iIndex = 0;iIndex<4;iIndex++) { // If the hexadecimal number is only one digit then convert the others to zero.e.g., if the length is 5, then convert to 0005 if ( cLengthOfString\[iIndex\] == 32 ) cLengthOfString\[iIndex\] = 48; // Store the length (which is in Hexa decimal format to the 6th, 7th, 8th and 9th characters of the file strContents.SetAt(iIndex+6,cLengthOfString\[iIndex\]); } cFilePointer.Close(); int iTemp = 0; int iIndexOfChar = 0; int iFileNameLength = strFileName.GetLength(); CString strSlash("\\\\"); // Add the slash to escape so that strFileName will have a proper path. while ( iTemp < iFileNameLength) { iIndexOfChar = strFileName.Find(strSlash,iTemp); if ( iIndexOfChar == -1 ) break; strFileName.Insert(iIndexOfChar,strSlash); iTemp = iIndexOfChar + 2; } // Re-create the file with the header information modified. hFile = CreateFile(strFileName,GENERIC\_WRITE,0,NULL,CREATE\_ALWAYS,FILE\_ATTRIBUTE\_NORMAL,NULL); if ( hFile ) cFilePointer.m\_hFile = hFile; if(cFilePointer) { cFilePointer.Write(strContents,iTotalLength); cFilePointer.Close(); }
}
Problem: When I executing the above code, the file is having every alternative character as NULL. Please help in the above. Thanks in advance, Neelesh K J Jain.
-
Hello Everybody, I am facing a strange problem. Requirement: Get the length of contents of a file and modify the header present in the file to represent the length in Hexadecimal format. Existing Code:
if( cFilePointer.Open(strFileName ,CFile::modeRead) )
{
strContents.Empty();
dwRead = 0;
// Read the contents of the file and store in the CString object.
do
{
dwRead = cFilePointer.Read(cBuffer,100);
if ( dwRead > 0)
strContents.operator +=(cBuffer);
}while(dwRead > 0 );// Retrieve the length of the string and convert to Hex decimal number int iTotalLength = strContents.GetLength(); sprintf(cLengthOfString,"%4x",iTotalLength); for ( int iIndex = 0;iIndex<4;iIndex++) { // If the hexadecimal number is only one digit then convert the others to zero.e.g., if the length is 5, then convert to 0005 if ( cLengthOfString\[iIndex\] == 32 ) cLengthOfString\[iIndex\] = 48; // Store the length (which is in Hexa decimal format to the 6th, 7th, 8th and 9th characters of the file strContents.SetAt(iIndex+6,cLengthOfString\[iIndex\]); } cFilePointer.Close(); int iTemp = 0; int iIndexOfChar = 0; int iFileNameLength = strFileName.GetLength(); CString strSlash("\\\\"); // Add the slash to escape so that strFileName will have a proper path. while ( iTemp < iFileNameLength) { iIndexOfChar = strFileName.Find(strSlash,iTemp); if ( iIndexOfChar == -1 ) break; strFileName.Insert(iIndexOfChar,strSlash); iTemp = iIndexOfChar + 2; } // Re-create the file with the header information modified. hFile = CreateFile(strFileName,GENERIC\_WRITE,0,NULL,CREATE\_ALWAYS,FILE\_ATTRIBUTE\_NORMAL,NULL); if ( hFile ) cFilePointer.m\_hFile = hFile; if(cFilePointer) { cFilePointer.Write(strContents,iTotalLength); cFilePointer.Close(); }
}
Problem: When I executing the above code, the file is having every alternative character as NULL. Please help in the above. Thanks in advance, Neelesh K J Jain.
Your project is a Unicode project, isn't it. That means you are writing Unicode (well, UTF-16) characters out to your file. Then, when you read with an ASCII file reader, you are reading the file as 8-bit characters, so you see the most significant byte of the UTF-16 characters as NULLs.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
Your project is a Unicode project, isn't it. That means you are writing Unicode (well, UTF-16) characters out to your file. Then, when you read with an ASCII file reader, you are reading the file as 8-bit characters, so you see the most significant byte of the UTF-16 characters as NULLs.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
Thank you Stuart, Can you please help in converting from ASCII to Unicode and vice versa, so that I don't face this problem in either of the conversion. Neelesh K J Jain.
-
Thank you Stuart, Can you please help in converting from ASCII to Unicode and vice versa, so that I don't face this problem in either of the conversion. Neelesh K J Jain.
This will answer more questions than you thought you had: http://www.codeproject.com/KB/string/cppstringguide1.aspx[^] Iain.
Codeproject MVP for C++, I can't believe it's for my lounge posts...
-
Thank you Stuart, Can you please help in converting from ASCII to Unicode and vice versa, so that I don't face this problem in either of the conversion. Neelesh K J Jain.
If you're using VC++2003, 2005 or 2008, it's very easy. If you #include , you have various string conversion macros[^] available. If you're using Visual C++ 6, you have similar macros[^] available.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p