Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如果行中有单词,请删除整行_List_C++ Cli - Fatal编程技术网

List 如果行中有单词,请删除整行

List 如果行中有单词,请删除整行,list,c++-cli,List,C++ Cli,我正在创建一个Windows窗体程序。在表单中,我通过文本框插入一些数据,然后将它们写入文件中。一旦我按下注册按钮,我注册一行,依此类推。 例如: 在另一种形式中,我在文本框中键入一个单词,如果这个单词在我的文件中,我想删除该行。 示例:单词是test5,因此我将删除它所在的所有行。我要: test1|test2|test3... 感谢Medinoc用户为此: 写 但是删除部分不能正常工作。你能帮我修一下吗 据我所知,删除函数只检查'cognome',因此如果您试图匹配的单词实际上是'nome

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

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

test1|test2|test3...
感谢Medinoc用户为此:


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

据我所知,删除函数只检查'cognome',因此如果您试图匹配的单词实际上是'nome',它将不匹配。尝试按如下方式修改删除功能:

if(obj->cognome != L"cat" && obj->nome != L"cat" )
    tw->WriteLine(line);
}

到底是什么问题?“无法正常工作”是一个非常模糊的错误描述。该函数应该在另一个文件中写入与“cat”不同的所有内容,然后覆盖原始文件。问题是函数删除了所有内容。顺便说一下,它应该是(obj->cognome!=textBox2->Text)tw->WriteLine(line);因为我想“保存”所有内容都与文本框中的文本不同。谢谢你的作品!如何从文本框中获取要比较的文本??我试过if(obj->cognome!=textBox2->Text)tw->WriteLine(line);但是没有结果
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");
if(obj->cognome != L"cat" && obj->nome != L"cat" )
    tw->WriteLine(line);
}