StreamReader StreamWriter只读写了一行
我换了一种方式不用转换类型了但有点问题FileStream^fileStream=gcnewFileStream(this->textBox9->Text,FileMod...
我换了一种方式 不用转换类型了 但有点问题
FileStream ^ fileStream = gcnew FileStream(this->textBox9->Text,FileMode::Open,FileAccess::Read);
StreamReader ^ streamReader=gcnew StreamReader(fileStream);
Console::WriteLine(streamReader->ReadLine());
while(!streamReader->EndOfStream)
this->richTextBox1->Text=streamReader->ReadLine();
FileStream ^ fs = gcnew FileStream("F:\\PP_AVO_Model_LamdaMu\\Debug\\1.txt",FileMode::Append);
StreamWriter ^ myWriter=gcnew StreamWriter(fs,System::Text::Encoding::GetEncoding("gb2312"));
myWriter->Write(this->richTextBox1->Text+"\r\n");
myWriter->Close();
streamReader->Close();
fileStream->Close();
读取内容的路径在textBox9里 放在1.txt后面 这样写后只读写进去第一行 我查了查网上都是while那句话没写 我这看不出问题在哪 展开
FileStream ^ fileStream = gcnew FileStream(this->textBox9->Text,FileMode::Open,FileAccess::Read);
StreamReader ^ streamReader=gcnew StreamReader(fileStream);
Console::WriteLine(streamReader->ReadLine());
while(!streamReader->EndOfStream)
this->richTextBox1->Text=streamReader->ReadLine();
FileStream ^ fs = gcnew FileStream("F:\\PP_AVO_Model_LamdaMu\\Debug\\1.txt",FileMode::Append);
StreamWriter ^ myWriter=gcnew StreamWriter(fs,System::Text::Encoding::GetEncoding("gb2312"));
myWriter->Write(this->richTextBox1->Text+"\r\n");
myWriter->Close();
streamReader->Close();
fileStream->Close();
读取内容的路径在textBox9里 放在1.txt后面 这样写后只读写进去第一行 我查了查网上都是while那句话没写 我这看不出问题在哪 展开
1个回答
展开全部
你上面代码对于2.txt,第一次读一行,然后写到控制台了
之后用while读,每读一行就把这行数据写到richTextBox1里(也就是说while结束的时候,richTextBox1里只有2.txt的最后一行数据,如果2.txt里原本只有一行数据的话,while进不去,richTextBox1里不会有数据),我搞不懂你为什么要把文件数据读到richTextBox1里,为了给人看?
然后你打开1.txt,把richTextBox1里的数据添加到1.txt,当然只能写进去一行了
看来还必须手把手教你:
FileStream ^ fileStream = gcnew FileStream(this->textBox9->Text,FileMode::Open,FileAccess::Read);
StreamReader ^ streamReader=gcnew StreamReader(fileStream);
FileStream ^ fs = gcnew FileStream("F:\\PP_AVO_Model_LamdaMu\\Debug\\1.txt",FileMode::Append);
StreamWriter ^ myWriter=gcnew StreamWriter(fs,System::Text::Encoding::GetEncoding("gb2312"));
while(!streamReader->EndOfStream)
{//读一行,写一行
myWriter->Write(streamReader->ReadLine());
myWriter->Write("\r\n");
}
myWriter->Close();
streamReader->Close();
fileStream->Close();
fs->Close();//这还少了这句
之后用while读,每读一行就把这行数据写到richTextBox1里(也就是说while结束的时候,richTextBox1里只有2.txt的最后一行数据,如果2.txt里原本只有一行数据的话,while进不去,richTextBox1里不会有数据),我搞不懂你为什么要把文件数据读到richTextBox1里,为了给人看?
然后你打开1.txt,把richTextBox1里的数据添加到1.txt,当然只能写进去一行了
看来还必须手把手教你:
FileStream ^ fileStream = gcnew FileStream(this->textBox9->Text,FileMode::Open,FileAccess::Read);
StreamReader ^ streamReader=gcnew StreamReader(fileStream);
FileStream ^ fs = gcnew FileStream("F:\\PP_AVO_Model_LamdaMu\\Debug\\1.txt",FileMode::Append);
StreamWriter ^ myWriter=gcnew StreamWriter(fs,System::Text::Encoding::GetEncoding("gb2312"));
while(!streamReader->EndOfStream)
{//读一行,写一行
myWriter->Write(streamReader->ReadLine());
myWriter->Write("\r\n");
}
myWriter->Close();
streamReader->Close();
fileStream->Close();
fs->Close();//这还少了这句
追问
大神!解决了!我再问问System::Text::Encoding::GetEncoding("gb2312")是干嘛的,还有原本有一句
Console::WriteLine(streamReader->ReadLine()); 是干嘛的
追答
System::Text::Encoding::GetEncoding("gb2312")
用gb2312(就是简体汉字标准)标准写入数据
Console::WriteLine(streamReader->ReadLine());
把sreamReader读到的一行数据写到控制台
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询