CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2012

    Question Trying to dump an "unsigned char" array to file!

    Hi there! How are you?

    My problem is fairly simple for those used to CLI programming!

    I basically have an "unsigned char" type array that I need to dump to a file and later be able to read it back, like an EEPROM memory. It's quite simple, but I am having difficulty to find the information on how that could be accomplished. I tried to use StreamWriter but I did not succeed. The code bellow compiles, but all I get is the "System.Byte[]" message on the created file...

    array<unsigned char>^ array3 = gcnew array<unsigned char>(8);
    String^ fileName = "textfile.txt";
    StreamWriter^ sw = gcnew StreamWriter(fileName);
    Edit: And ah! After I succeed writing it, how to read it back??

    Any help will be greatly appreciated!

    Last edited by fernando306; December 27th, 2012 at 10:35 AM.

  2. #2
    Join Date
    Jun 2010

    Re: Trying to dump an "unsigned char" array to file!

    Quote Originally Posted by fernando306 View Post
    Hi there! How are you?
    Fine. (For the unlikely case this question is actually meant for me... )

    Looks like what you actually want is a FileStream rather than a StreamWriter. Like this:

    String ^fileName = "textfile.txt";
    FleStream ^fs = gcnew FileStream(fileName, FileMode::Create);
    fs->Write(array3, 0, array3->Length);
    Already the first line of the MSDN StreamWriter doc page could have been a useful indicator that this isn't what you actually want, BTW:

    Quote Originally Posted by MSDN
    Implements a TextWriter for writing characters to a stream in a particular encoding.
    [...] And ah! After I succeed writing it, how to read it back??
    Reading it back is much like writing it; you'd even use the same framework class. Just study the MSDN docs.

    However, if what you actually want is text file handling, a StreamWriter may very well be what you're looking for. It just doesn't have a Write(array<unsigned char> ^) overload (the C++ unsigned char is equivalent to the .NET System::Byte). Appropriate objects to pass to TextWriter::Write() are, among others, array<System::Char> ^ (note the capital C, where the equivalent native item type is wchar_t rather than unsigned char) or simply System::String ^.

    BTW, code tags?
    Last edited by Eri523; December 27th, 2012 at 11:21 AM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jun 2012

    Re: Trying to dump an "unsigned char" array to file!

    Hi Eri523! Thank you so much for your help. I'll definitively study the FileStream class and implement it. No, I'm not handling text files, so you surely saved me from a lot of headaches

    Oh yeah! Code tags!

    I'll get back here ASAP. Happy Xmas BTW!

  4. #4
    Join Date
    Jun 2012

    Re: Trying to dump an "unsigned char" array to file!

    Hey Eri523!

    I used FileStream and it worked 100% fine. I also used OpenFileDialog and SaveFileDialog to browse directories and provide the file path. It was a piece of cake!

    Thanks a lot, your suggestion was great!

    Best wishes,


Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.