我读文件和写文件是这样写的: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#
{
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#
自问自答。关于第一个问题是因为FileStream^ fs = File::Create(str);你每次读文件都新建了一个同样的文件覆盖了。换个OpenRead(str)应该就可以了。
心情不好(今天失恋了)的时候喜欢在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个元素赋值