I'm new to C++ and CLI/C++ and I'm having a problem. I would like to replace the line in a textfile with different text of my choosing. For example, I have a text file with 12 lines and I want to replace the 6th line, that says "Line 6" with the text "Line 6 edited". Now, I've only started learning about handling text files 1 day ago and I have figured out how to insert lines, so all the code I have at the moment is this:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
char txtfile[]="C:\\Users\\acer\\Desktop\\aaa.txt";
StreamWriter ^writer = gcnew StreamWriter(gcnew String(txtfile),true);
writer->WriteLine("Line 1");
writer->WriteLine("Line 2");
writer->Close();
delete writer;
}
From what I researched, I found out I have 2 methods of doing this. I could use a temp file or I can load all the lines into an array and store it in memory if the file is not too big. And the File is 1KByte so I would like to use this 2nd method of storing it in memory. Now, my problem is that since I'm new to this language I have no idea how to actually do this. And I have been searching to see if I can find instructions on how to do this in CLI C++ but I can't find anything. I've been looking at this topic here--> Delete specific line from a text file? But's it's in C# and I don't know how to convert the code to CLI C++.
EDIT : I have seen this piece of C# code from that other topic that shows how to delete a line and I would like to try it
var file = new List<string>(System.IO.File.ReadAllLines("C:\\path"));
file.RemoveAt(12);
File.WriteAllLines("C:\\path", file.ToArray());
I tried to convert to something like this in CLI/C++:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
char txtfile[]="C:\\Users\\acer\\Desktop\\aaa.txt";
array<String^>^ fileArray = System::IO::File->ReadAllLines(txtfile);
fileArray->Remove(6);
File->WriteAllLines(txtfile, fileArray->ToArray());
}
But I had no success because I don't know the correct syntax for CLI/C++ since I'm newbie. How can I use that C# code in CLI C++? I really appreciate the instructions but I need code examples in CLI/C++.