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! :D
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!
Re: Trying to dump an "unsigned char" array to file!
Quote:
Originally Posted by
fernando306
Hi there! How are you?
Fine. :) (For the unlikely case this question is actually meant for me... :D)
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.
Quote:
[...] 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? ;)
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! :D
I'll get back here ASAP. Happy Xmas BTW! :D
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