如何读取输出文件的行号并转到输入文件中的该行号

时间:2015-02-26 11:37:42

标签: c++-cli windows-forms-designer

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

我希望能够读取输出文件所在的行号,因此当我关闭应用程序并返回它时,我不必再次启动整个输入文件,而是可以转到我是最后一次。

这是我的代码:

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;

1 个答案:

答案 0 :(得分:0)

在没有读取整个文件并且计算换行符的情况下,无法跳转到行号。

相反,使程序保存输入文件中的字节偏移量,并在下次运行时跳转到该文件。使用Stream::PositionStream::Seek即可。