开发者

Why am I getting gibberish output, along with valid output, when reading a String^ ?

开发者 https://www.devze.com 2023-03-01 17:33 出处:网络
I\'m trying to write a few integers to a file (as a string.) Every time I try to run this bit of code I get the integers into the text file like planned, but before the integers, I get some gibberish.

I'm trying to write a few integers to a file (as a string.) Every time I try to run this bit of code I get the integers into the text file like planned, but before the integers, I get some gibberish. I did some experimenting, and found out that if I put nothing into System::String ^ b, it would give the same gibberish output into the file or a message box, but I couldn't figure out why it would do this if I was concatenating those integers to it (as strings). What could be going wrong here?

using namespace msclr::interop;
using namespace System;
using namespace System::IO;
using namespace System::Text;

...

System::IO::StreamWr开发者_如何学Citer ^ x;
char buffer[21], buffer2[3];
int a;
for(a = 0; a < 10; a++){
    itoa(weight[a], buffer, 10);
    strcat(buffer, buffer2);
}
System::String ^ b = marshal_as<String^>(buffer);
x->WriteLine(b);


What format is the file in? You may be reading a UTF-8 file with a byte-order mark that is silently applied by a text editing program.

http://en.wikipedia.org/wiki/Byte_order_mark


Typo in question or bug in code: pass buffer2 to itoa instead of buffer.

Also, initialize buffer to "";

0

精彩评论

暂无评论...
验证码 换一张
取 消