none
Mege Tow or more text files RRS feed

  • Question

  • hi!

    how can i merge tow or more text files in visual c++ with a button


    • Edited by chihebmad Wednesday, January 23, 2019 1:53 PM
    Wednesday, January 23, 2019 1:53 PM

Answers

  • Hi chihebmad,

    To merge file, you can refer to the following code. Before this, you need to add the headfile of "<fstream>".

    	private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
    		richTextBox1->LoadFile("D:\\text\\1.txt", RichTextBoxStreamType::PlainText);
    		richTextBox2->LoadFile("D:\\text\\2.txt", RichTextBoxStreamType::PlainText);
    	}
    	private: System::Void btMerge_Click(System::Object^  sender, System::EventArgs^  e) {
    		char c[10000];
    		std::ifstream ifile1("D:\\text\\1.txt");
    		std::ifstream ifile2("D:\\text\\2.txt");
    		std::ofstream ofile("D:\\text\\3.txt");
    
    		while (!ifile1.eof())
    		{
    			ifile1.getline(c, 9999);
    			ofile << c << std::endl;
    		}
    
    		while (!ifile2.eof())
    		{
    			ifile2.getline(c, 9999);
    			ofile << c << std::endl;
    		}
    		ifile1.close();
    		ifile2.close();
    		ofile.close();
    		richTextBox3->LoadFile("D:\\text\\3.txt", RichTextBoxStreamType::PlainText);
    	}

    In this demo, it uses three richtextboxes to show two existing "txt" and the new merged "txt".

    Here is the test result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by chihebmad Friday, January 25, 2019 6:39 PM
    Thursday, January 24, 2019 3:14 AM
    Moderator

All replies

  • Hello, 

    One of the options - you study shell COPY command and run it with required params.


    Sincerely, Highly skilled coding monkey.

    Wednesday, January 23, 2019 2:44 PM
  • Hi chihebmad,

    To merge file, you can refer to the following code. Before this, you need to add the headfile of "<fstream>".

    	private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
    		richTextBox1->LoadFile("D:\\text\\1.txt", RichTextBoxStreamType::PlainText);
    		richTextBox2->LoadFile("D:\\text\\2.txt", RichTextBoxStreamType::PlainText);
    	}
    	private: System::Void btMerge_Click(System::Object^  sender, System::EventArgs^  e) {
    		char c[10000];
    		std::ifstream ifile1("D:\\text\\1.txt");
    		std::ifstream ifile2("D:\\text\\2.txt");
    		std::ofstream ofile("D:\\text\\3.txt");
    
    		while (!ifile1.eof())
    		{
    			ifile1.getline(c, 9999);
    			ofile << c << std::endl;
    		}
    
    		while (!ifile2.eof())
    		{
    			ifile2.getline(c, 9999);
    			ofile << c << std::endl;
    		}
    		ifile1.close();
    		ifile2.close();
    		ofile.close();
    		richTextBox3->LoadFile("D:\\text\\3.txt", RichTextBoxStreamType::PlainText);
    	}

    In this demo, it uses three richtextboxes to show two existing "txt" and the new merged "txt".

    Here is the test result:

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by chihebmad Friday, January 25, 2019 6:39 PM
    Thursday, January 24, 2019 3:14 AM
    Moderator