1

我正在创建一个 Windows 窗体程序。在表单中,我通过文本框插入一些数据,然后将它们写入文件。一旦我按下注册按钮,我就会注册一条线路,依此类推。例子:

test1|test2|test3...

test4|test5|test6...

在另一种形式中,我在文本框中输入一个词,如果这个词在我的文件中,我想删除该行。示例:单词是test5,所以我将删除它所在的所有行。我会有:

test1|test2|test3...

感谢 Medinoc 用户:

ref class MyClass
{
public:
    String^ cognome;
    String^ nome;
    int voto_diploma;
};

//...

List<MyClass^>^ primo = gcnew List<MyClass^>();

//...

MyClass^ myObj = gcnew MyClass();
myObj->cognome = textBox1->Text;
myObj->nome = textBox2->Text;
myObj->voto_diploma = Convert::ToInt32(textBox35->Text);
primo->Add(myObj);

//...

TextWriter ^tw = gcnew StreamWriter(L"primoAnno.txt", true);
for each(MyClass^ obj in primo)
{
//You can use any character or string as separator,
//as long as it's not supposed to appear in the strings.
//Here, I used pipes.
tw->Write(obj->cognome);
tw->Write(L"|");
tw->Write(obj->nome);
tw->Write(L"|");
tw->WriteLine(obj->voto_diploma);
}
tw->Close();

MyClass^ ParseMyClass(String^ line)
{
array<String^>^ splitString = line->Split(L'|');
MyClass^ myObj = gcnew MyClass();
myObj->cognome = splitString[0];
myObj->nome = splitString[1];
myObj->voto_diploma = Convert::ToInt32(splitString[2]);
return myObj;
}

删除

TextWriter^ tw = gcnew StreamWriter(L"primoAnno2.txt", true);
TextReader^ tr = gcnew StreamReader(L"primoAnno.txt");
String^ line;
while((line=tr->ReadLine()) != nullptr)
{
MyClass^ obj = ParseMyClass(line);
if(obj->cognome != L"cat")
    tw->WriteLine(line);
}
tr->Close();
tw->Close();
File::Delete(L"primoAnno.txt");
File::Move(L"primoAnno2.txt", L"primoAnno.txt");

但删除部分不能正常工作。你能帮我修一下吗?

4

1 回答 1

0

据我所知,删除功能仅检查“认知”,因此如果您尝试匹配的单词实际上是“名称”,它将不匹配。尝试像这样修改删除功能:

if(obj->cognome != L"cat" && obj->nome != L"cat" )
    tw->WriteLine(line);
}
于 2013-07-04T15:45:02.013 回答