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
    Posts
    37

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

    Code:
    array<unsigned char>^ array3 = gcnew array<unsigned char>(8);
    
    array3[0]=10;
    array3[1]=77;
    array3[2]=24;
    array3[3]=56;
    array3[4]=17;
    array3[5]=76;
    array3[6]=72;
    array3[7]=05;
    
    String^ fileName = "textfile.txt";
    StreamWriter^ sw = gcnew StreamWriter(fileName);
    sw->Write(array3);
    sw->Close();
    Edit: And ah! After I succeed writing it, how to read it back??

    Any help will be greatly appreciated!

    Thanks!
    Last edited by fernando306; December 27th, 2012 at 11:35 AM.

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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:

    Code:
    String ^fileName = "textfile.txt";
    FleStream ^fs = gcnew FileStream(fileName, FileMode::Create);
    fs->Write(array3, 0, array3->Length);
    fs->Close();
    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 12:21 PM.
    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
    Posts
    37

    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
    Posts
    37

    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,

    Fernando

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