0

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++.

Community
  • 1
  • 1
sparrow.
  • 101
  • 8
  • There are two common solutions to this sort of problem: 1) read the entire file into an array of strings [or something like that], make your edits to the strings applicabale, and write back the result to the original file. 2) open a second output file, read each line, change the input string, and write to a output file. – Mats Petersson Aug 22 '13 at 14:54
  • Thank you for such a fast response. But like I said on my question, I am aware of the options I have for doing this. My actual problem is the code. Since I'm new to this language I don't know how I can do it. I looked at the C# code posted on that topic I mentioned but I don't how to convert it to C++/CLI – sparrow. Aug 22 '13 at 15:06
  • You should create a temporary file anyway: Read the original file into memory (maybe with adjustments applied, already) - adjust that data (if not already done) - write a temporary file - rename the original - rename the temporary file to the original name - erase the renamed orginal –  Aug 22 '13 at 15:07
  • When dealing with text, you have to be aware of character sets and encodings. .NET generally uses the Unicode character set and UTF-16 encoding. For files, Unicode and UTF-8 is common, as is Windows-1252 (or similar). The `StreamWriter` and `StreamReader` classes use UTF-8 unless another character set and encoding are specified. Since you are working with an existing file, you need to determine the character set and encoding to use while reading and writing, and also not attempt to write characters that aren't supported by that character set. – Tom Blodget Aug 22 '13 at 16:04
  • Alright, I don't think that will be a problem since all the text will be in plain english so I won't be using any weird characters. @DieterLücking I see what you mean but reading the entire file into a array of strings and removing/adding lines seems to be simpler. I edited my question and added the C# code to remove a line, I just need to convert it to CLI/C++. Although if you can provide a code example of the method you speaked of, I would be very grateful and try to use it. – sparrow. Aug 22 '13 at 18:33

1 Answers1

0

You seem to be confusing static and non-static methods:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    System::String^ txtfile = L"C:\\Users\\acer\\Desktop\\aaa.txt";
    array<System::String^>^ lineArray = System::IO::File::ReadAllLines(txtfile);
    System::Collections::Generic::List<System::String^>^ lineList;
    lineList = gcnew System::Collections::Generic::List<System::String^>(lineArray);
    lineList->RemoveAt(6);
    System::IO::File::WriteAllLines(txtfile, fileList->ToArray());
 }
Medinoc
  • 6,577
  • 20
  • 42
  • Thank you, I tried that but I got a few errors. It told me "fileList" was an undeclared identifier, I assumed you meant lineList and replaced it but I also had another error on the line "lineList->Remove(6);" It says this: **error C2664: 'System::Collections::Generic::List::Remove' : cannot convert parameter 1 from 'int' to 'System::String ^'** – sparrow. Aug 23 '13 at 20:26
  • Thank you that worked after I replaced "fileList" with lineList" on that last line. Something weird is happening now which is a minor problem, but still. It's removing line number 7 even though the code says line number 6. – sparrow. Aug 24 '13 at 00:14
  • Nevermind about that issue I mentioned, I just realized that line 1 is line 0 and line 2 is line 1 and so on. Thanks for your help. – sparrow. Aug 24 '13 at 00:29