CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    write CString to binary file

    Hi,

    How to write some data contained in a CString to a binary file?
    The CString data can only contain "hex values", i.e. str = "010145b4bd".
    When I write using ofstream or CFile and open the file with a hex editor it shows my string hex values in the ascii part :

    00000000 | 30 31 30 31 34 | 010145b4bd

    any idea?
    thanks in advance
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  2. #2
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: write CString to binary file

    ok, i found a solution.

    1) convert the CString to unsigned char
    2) write the unsigned char buffer to the file
    Code:
    	
    CFile f( headerPath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
    CString header = "all hex bytes here";
    int sz = header.GetLength();
    unsigned char Msg[5000] = "";
    unsigned char* pMsg = Msg;
    CStringToHex(header, pMsg); //(this is my own conversion function)	
    f.Write(pMsg,sz);
    f.Close();
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: write CString to binary file

    Sounds like you use "ofstream or CFile" not correct, but without seeing your code...
    Code:
    // non-unicode build
    CFile file;
    CString str("010145b4bd");
    if(fle.Open(....))
    {
            file.Write((LPCSTR)str, str.GetLength());
            file.Close();
    }

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: write CString to binary file

    step 1 can be fixed with MyString.GetBuffer (0). This gives you the internal pointer to the charbuffer of the CString.

  5. #5
    Join Date
    Aug 2001
    Location
    germany
    Posts
    772

    Re: write CString to binary file

    Thanks!

    victor, yeah, i didn't use LPCSTR.
    Please rate if it helps!

    * The second mouse gets the cheese.

    * Birthdays are good for you. The more you have, the longer you live.

    * Always keep your words soft and sweet, just in case you have to eat them.

    * Always read stuff that will make you look good if you die in the middle of it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured