title: How do I replace a certain line on a text file with different text?
body: <p>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:</p>
  
  <pre><code>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-&gt;WriteLine("Line 1");
                  writer-&gt;WriteLine("Line 2");
                  writer-&gt;Close();
                  delete writer;
           }
  </code></pre>
  
  <p>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--> <a href="https://stackoverflow.com/questions/1245243/delete-specific-line-from-a-text-file">Delete specific line from a text file?</a>
  But's it's in C# and I don't know how to convert the code to CLI C++.</p>
  
  <p><strong>EDIT :</strong> 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 </p>
  
  <pre><code>var file = new List&lt;string&gt;(System.IO.File.ReadAllLines("C:\\path"));
          file.RemoveAt(12);
          File.WriteAllLines("C:\\path", file.ToArray());
  </code></pre>
  
  <p>I tried to convert to something like this in CLI/C++:</p>
  
  <pre><code>private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
                  char txtfile[]="C:\\Users\\acer\\Desktop\\aaa.txt";
                  array&lt;String^&gt;^ fileArray = System::IO::File-&gt;ReadAllLines(txtfile);
                  fileArray-&gt;Remove(6);
                  File-&gt;WriteAllLines(txtfile, fileArray-&gt;ToArray());
                  }
  </code></pre>
  
  <p>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++.</p>