如何保存字节偏移量并使用stream :: seek或stream :: position

时间:2015-02-27 15:43:19

标签: c++-cli

我有一个程序,它逐行浏览输入文件,在Windows窗体上显示,然后是一个动作按钮,将结果发布到输出文件。

我希望能够使用stream :: seek,所以当我关闭应用程序并返回它时,它会转到我输入的输入的最后一行,所以我不必每次都从头开始。

这是我目前的代码:

static System::IO::StreamReader ^fileName = nullptr;   


    public: System::Void StartBtn_Click(System::Object^  sender, System::EventArgs^  e) {
count++;

if (count == 1)
{
                textBox1->Text="";
                textBox2->Text="";
                textBox3->Text="";
                textBox4->Text="";
                textBox5->Text="";
if (fileName == nullptr)

        fileName = gcnew System::IO::StreamReader (textBox6->Text);



 if (!fileName->EndOfStream)
 {
  String^ delimStr = ",";

   array<Char>^ delimiter = delimStr->ToCharArray( );
   array<String^>^ words;
   String^ str = fileName->ReadLine();


words = str->Split( delimiter );



     textBox1->Text += gcnew String (words[10]);


     textBox2->Text += gcnew String (words[21]);

     textBox3->Text += gcnew String (words[49]);

     textBox4->Text += gcnew String (words[0]);

     textBox5->Text += gcnew String (words[28]);




 }
}
 else 
 {
                textBox1->Text="";
                textBox2->Text="";
                textBox3->Text="";
                textBox4->Text="";
                textBox5->Text="";
if (fileName == nullptr)
        fileName = gcnew System::IO::StreamReader (textBox6->Text);



 if (!fileName->EndOfStream)
 {
   String^ delimStr = ",";

   array<Char>^ delimiter = delimStr->ToCharArray( );
   array<String^>^ words;
   String^ str = fileName->ReadLine();


   words = str->Split( delimiter );




     textBox1->Text += gcnew String (words[10]);


     textBox2->Text += gcnew String (words[21]);

     textBox3->Text += gcnew String (words[49]);

     textBox4->Text += gcnew String (words[0]);

     textBox5->Text += gcnew String (words[28]);



String^ fileName1 = nullptr;
                 System::String^ finaladdress = textBox1->Text; 

                 System::String^ correctString = finaladdress->Replace( "\r\n", "," );

                 System::String^ finaladdress2 = textBox2->Text;    

                 System::String^ correctString2 = finaladdress2->Replace( "\r\n", "," );

                 System::String^ finaladdress3 = textBox3->Text;    

                 System::String^ correctString3 = finaladdress3->Replace( "\r\n", "," );

                 System::String^ Action = ReqActBox->SelectedItem->ToString();

                 System::String^ warnings = textBox4->Text;
        System::String^ correctString4 = warnings->Replace( "\r\n", "," );

                 System::String^ warnings1 = textBox5->Text;
        System::String^ correctString5 = warnings1->Replace( "\r\n", "," );

                 fileName1 += "C:\\MergeData\\output.txt";
                 StreamWriter^ sw = gcnew StreamWriter(fileName1, true);


      sw->Write(gcnew String ("\r\n"));              
      sw->Write(gcnew String (words[4]));
      sw->Write(gcnew String (","));
      sw->Write(Action);
      sw->Write(gcnew String (","));

      sw->Write(correctString);
      sw->Write(gcnew String (","));
          sw->Write(correctString4);
      sw->Write(gcnew String (","));
          sw->Write(correctString2);
          sw->Write(gcnew String (","));
      sw->Write(correctString5);
      sw->Write(gcnew String (","));
          sw->Write(correctString3);

          sw->Close();


 }

   }


}


private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

   System::String^ paf = textBox6->Text;
  OpenFileDialog^ sfd = gcnew OpenFileDialog();

            if( sfd->ShowDialog() == System::Windows::Forms::DialogResult::OK )
            {
                paf = sfd->FileName;
            }


    textBox6->Text = paf;

0 个答案:

没有答案