I would like to know how can I search a line by its text on a textfile and delete it. After looking at this topic-->Delete specific line from a text file? I found out this code in C#:
string line = null;
string line_to_delete = "the line i want to delete";
using (StreamReader reader = new StreamReader("C:\\input")) {
using (StreamWriter writer = new StreamWriter("C:\\output")) {
while ((line = reader.ReadLine()) != null) {
if (String.Compare(line, line_to_delete) == 0)
continue;
writer.WriteLine(line);
}
}
}
I tried to convert it to something like this in C++/CLI:
System::String^ txtfile = L"C:\\Users\\acer\\Desktop\\aaa.txt";
String^ line = nullptr;
String^ line_to_delete = "dasdasdasda";
using (StreamReader ^reader = gcnew StreamReader(gcnew String(txtfile)) {
using (StreamWriter ^writer = gcnew StreamWriter(gcnew String(txtfile),true) {
while ((line == reader->ReadLine()) != nullptr) {
if (String.Compare(line, line_to_delete) == 0)
continue;
writer->WriteLine(line);
}
}
}
but since I'm still a newbie I didn't do it right. Please notice that I did not create any temporary file, I just want to read my textfile, detect the line that says "dasdasdasda" and delete it. Can someone tell what I did wrong when converting from C# to C++/CLI?