扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:谢启东编译 来源:天极开发 2007年11月14日
关键字:
using namespace System; using namespace System::IO; void Copy(TextReader^ inStream, TextWriter^ outStream); int main(array<String^>^ argv) { if (argv->Length != 2) { Console::WriteLine("需要两个参数。"); /*1*/ Environment::Exit(1); } try { /*2a*/ FileStream^ inFile = gcnew FileStream(argv[0], FileMode::Open); /*2b*/ StreamReader^ inStream = gcnew StreamReader(inFile); /*2c*/// StreamReader^ inStream = File::OpenText(argv[0]); Console::WriteLine("CanRead is {0}, CanWrite is {1}",inFile->CanRead, inFile->CanWrite); /*3*/ StreamWriter^ outStream = File::CreateText(argv[1]); /*4*/ Copy(inStream, outStream); /*5*/ outStream->Write("{0} * {1} = {2}\n", 10, 5, 10 * 5); inStream->Close(); outStream->Close(); } /*6*/ catch (FileNotFoundException^ ex) { Console::WriteLine(ex->Message); } /*7*/ catch (IOException^ ex) { Console::WriteLine(ex); } } /*8*/ void Copy(TextReader^ inStream, TextWriter^ outStream) { int c; while ((c = inStream->Read()) != -1) { outStream->Write(static_cast<wchar_t>(c)); } } |
using namespace System; using namespace System::Text; using namespace System::IO; void Copy(TextReader^ inStream, TextWriter^ outStream); int main() { String^ str = "abcde"; /*1*/ StringReader^ inStream = gcnew StringReader(str); /*2*/ StringWriter^ outStream = gcnew StringWriter; /*3*/ StringBuilder^ sb = outStream->GetStringBuilder(); Console::WriteLine("Capacity is {0}", sb->Capacity); /*4*/ outStream->Write(static_cast<wchar_t>(inStream->Read())); //读写a outStream->Write('!'); // write a ! outStream->Write(static_cast<wchar_t>(inStream->Read())); //读写b outStream->Flush(); outStream->Write("Result = {0,4:0.##}", 10.0/3); //输出格式化文本 /*5*/ Console::WriteLine(outStream); //调用StringWriter::ToString /*6*/ Copy(inStream, outStream); /*7*/ Console::WriteLine(outStream); /*8*/ Console::WriteLine(sb); //调用StringBuilder::ToString /*9*/ inStream->Close(); outStream->Close(); } void Copy(TextReader^ inStream, TextWriter^ outStream) { int c; while ((c = inStream->Read()) != -1) { outStream->Write(static_cast<wchar_t>(c)); } } |
Capacity is 16 a!bResult = 3.33 a!bResult = 3.33cde a!bResult = 3.33cde |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者