C++ cli 如何读取输出文件的行号并转到输入文件中的行号
我有一个程序,它逐行遍历一个输入文件,将其显示在windows窗体上,然后是一个操作按钮,将结果发布到输出文件 我希望能够读取输出文件所在的行号,这样当我关闭应用程序并返回到它时,我就不必再次启动整个输入文件,而是可以转到上次打开的文件 这是我的代码:C++ cli 如何读取输出文件的行号并转到输入文件中的行号,c++-cli,windows-forms-designer,C++ Cli,Windows Forms Designer,我有一个程序,它逐行遍历一个输入文件,将其显示在windows窗体上,然后是一个操作按钮,将结果发布到输出文件 我希望能够读取输出文件所在的行号,这样当我关闭应用程序并返回到它时,我就不必再次启动整个输入文件,而是可以转到上次打开的文件 这是我的代码: static System::IO::StreamReader ^fileName = nullptr; public: System::Void StartBtn_Click(System::Object^ sender,
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;
在不读取整个文件并计算换行数的情况下,无法跳转到行号
取而代之的是,使您的程序在输入文件中保存字节偏移量,并在下次运行时跳转到该偏移量。使用或执行此操作。谢谢David!你能给我举个例子说明怎么做吗?我在网上找不到。