2013-07-04 2 views
1

Windows 양식 프로그램을 만들고 있습니다. 양식에서 텍스트 상자를 통해 일부 데이터를 삽입하고이를 파일에 씁니다. 일단 내가 등록 버튼을 누르면 나는 등등을 등록한다. 예 : 또 다른 형태에서단어가있는 경우 전체 줄 삭제

test1|test2|test3... 

test4|test5|test6... 

나는 텍스트 상자에 단어를 입력하고이 단어가 내 파일에있는 경우 그 줄을 삭제하고 싶습니다. 예 : 단어가 test5이므로 모든 줄이 삭제됩니다. 내가해야합니다 : Medinoc 사용자에게

test1|test2|test3... 

감사이를 위해 :

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; 
} 

읽어

WRITE 삭제

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"); 

하지만 삭제 부분이 제대로 작동하지 않습니다. 고칠 수있게 도와 주시겠습니까?

+0

정확히 무엇이 문제입니까? "올바르게 작동하지 않습니다"는 매우 모호한 오류 설명입니다. – PeterK

+0

이 함수는 모든 파일을 다른 파일에 "cat"과 다르게 작성한 다음 원본 파일을 덮어 씁니다. 문제는 함수가 모든 내용을 삭제한다는 것입니다. 그건 그렇고해야합니다 (obj-> cognome! = textBox2-> Text) tw-> WriteLine (line); 나는 "저장"하고 싶습니다. everithig는 textbox.thanks의 텍스트와 다릅니다. – gAeT

답변

0

내가 말할 수있는 한, 삭제 기능은 'cognome'만 검사하므로 일치하려는 단어가 실제로 'nome'인 경우 일치하지 않습니다. 다음과 같이 삭제 기능을 수정 해보십시오.

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

이 작동합니다! 어떻게 텍스트를 텍스트 상자에서 비교할 수 있습니까 ?? 나는 시도했다 (obj-> cognome! = textBox2-> Text) tw-> WriteLine (line); 그러나 아무 결과도없이 – gAeT

+0

나는 어리석은 실수를 저질 렀다. 잘 작동합니다! – gAeT

관련 문제