質問

How do I add to an existing text file? The following erases whatever is on the text file. I could do this in C++ but I'm new to C++/CLI.

    public ref class form : public System::Windows::Forms::Form
                {
                public:
                    form(void)
                    {
                        InitializeComponent();
                        void save(String^ word);

                    }
                       //windows form generated code
                        ...
                        ...
                        ...
                        void save(String^ word)
                {
                    StreamWriter^ outFile = gcnew StreamWriter("file.txt");
                    outFile->WriteLine(word);
                    outFile->Close();
                }
        #pragma endregion
             private: System::Void button00_Click(System::Object^  sender, System::EventArgs^  e) {
                         String^ word = "PLOW";
                         save(word);
            }
    };
    }
役に立ちましたか?

解決

One of the constructors of the StearWriter class has a boolean called "Append". set it to true, this should append the content to the end of the stream instead of overwriting it. basically, all you have to do is replace this code line

StreamWriter^ outFile = gcnew StreamWriter("file.txt");

with this one:

StreamWriter^ outFile = gcnew StreamWriter("file.txt", true);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top