Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Every Alternative Character is NULL while writing to a file

Every Alternative Character is NULL while writing to a file

Scheduled Pinned Locked Moved C / C++ / MFC
help
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neelesh K J Jain
    wrote on last edited by
    #1

    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.

    S 1 Reply Last reply
    0
    • N 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.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      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

      N 1 Reply Last reply
      0
      • S Stuart Dootson

        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

        N Offline
        N Offline
        Neelesh K J Jain
        wrote on last edited by
        #3

        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.

        I S 2 Replies Last reply
        0
        • N 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.

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          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...

          1 Reply Last reply
          0
          • N 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.

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups