我读文件和写文件是这样写的:void clrClass::MemberSave(array<int>^MyIntArray,String^ str)
   {
int i=0;
try
{
FileStream^ fs = File::Create(str);
    StreamWriter^ sw=gcnew StreamWriter(fs);
   /*BinaryWriter^ bw=gcnew BinaryWriter(fs);*/
   while(i<10)
  {
 sw->Write(MyIntArray[i++]+" ");
  }
  sw->Close();
}
catch(Exception^ e)
{
Console::WriteLine( "The file could not be open" );
        Console::WriteLine( e->Message );
}

   }   void clrClass::MemberLoad(array<int>^MyIntArray,String^ str)
   {
int i=-1;
try
{
   FileStream^ fs = File::Create(str);
   StreamReader^ sr=gcnew StreamReader(fs);
   while(MyIntArray[i]=sr->Read())
  {
i=i+2;
  }
   sr->Close();
}
catch(Exception^ e)
{
Console::WriteLine( "The file could not be read:" );
        Console::WriteLine( e->Message );
}   }
第一个问题是为什么streamreader读完后文件变空了?
第二个问题是streamreader的read()方法说道Reads the next character from the input stream and advances the character position by one character.他说的提升一个字符是什么意思?
第三个问题是i为什么从-1开始才能读到所有的内容。从0开始只隔着读一半?
第四个小问题是read()方法读到的应该是字符串。而MyIntArray[i]是一个托管数组。为什么可以直接赋值?从类到数值??streamreaderc#

解决方案 »

  1.   

    这段我写的代码有意思了。对于第三个问题我加了try{}catch(){}但是我catch里的东西写错了。我写的不是控制台程序。有错但是在winform调用时不会提示,反而让我以为我是对的。i怎么能从-1开始呢??i是数组下标。坑爹了。。还是读文件的问题。这是没怎么学过c#的恶果啊。
      

  2.   


    自问自答。关于第一个问题是因为FileStream^ fs = File::Create(str);你每次读文件都新建了一个同样的文件覆盖了。换个OpenRead(str)应该就可以了。
      

  3.   

    主要问题太多了 
    心情不好(今天失恋了)的时候喜欢在CSDN打发时间
    1、 FileStream^ fs = File::Create(str); 读的时候是打开文件 不是创建
    2、Reads the next character from the input stream and advances the character position by one character. 就是从流中一个一个读字符
    3、先把第一个问题改了  应该i 0到9就行了
    4、给数组给i个元素赋值