我想知道如何通过文本文件中的文本搜索一行并将其删除。在查看了这个主题之后-->从文本文件中删除特定行?我在 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);
}
}
}
我试图在 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);
}
}
}
但由于我还是个新手,所以我做得不对。请注意,我没有创建任何临时文件,我只是想阅读我的文本文件,检测“dasdasdasda”的行并将其删除。有人能告诉我从 C# 转换为 C++/CLI 时我做错了什么吗?