none
c++文件拷贝问题 RRS feed

  • 问题

  • //程序实现功能:将D盘里的两个文件(1.txt和2.txt)中的内容复制到D盘里的a.txt文档中
    //问题:没有实现预订的功能,请问为什么?
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main(void)
    {	ofstream out;
    	ifstream in;
    	char ch;
    	out.open("D:\\a.txt",ios::app);
    	if (!out) { cout<<"out ERROR!"<<endl; }
    	in.open("D:\\1.txt",ios::in);
    	if (!in) { cout<<"1 ERROR!"<<endl; }
    	while( in.get(ch) )	{	out<<ch;	}
    	out<<ch;
    	in.close();
    	in.open("D:\\2.txt",ios::in);
    	if (!in) { cout<<"2 ERROR!"<<endl; }
    	while( in.get(ch) )	{	out<<ch;	}	
            out<<ch;
    	in.close();
    	out.close();
    	return 0;
    }
    2011年4月28日 14:12

答案

  • 在您调用in.open("D:\\2.txt",ios::in);的前面加上in.clear();试试


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年4月28日 14:36
    版主
  • 1out<<ch; 改为out<<ch<<endl;

    2如果还不行,尝试使用wifstream和woifstream


    麻烦把正确答案设为解答。
    2011年4月29日 1:54
    版主
  • 吴岚,

    您好!如果您想实现D盘里的两个文件(1.txt2.txt)中的内容复制到D盘里的a.txt文档中。您可以使用以下代码。

    #include "stdafx.h"

    #include <fstream>

    #include <iostream>

     

    using namespace std;

     

    bool copyFile (const char SRC1[],const char SRC2[], const char DEST[])

    {

        std::ifstream src1; // the source file1

          std::ifstream src2; // the source file2

        std::ofstream dest; // the destination file

     

        src1.open (SRC1, std::ios::binary); // open in binary to prevent jargon at the end of the buffer

          src2.open (SRC2, std::ios::binary); // open in binary to prevent jargon at the end of the buffer

        dest.open (DEST, std::ios::binary); // same again, binary

        if (!src1.is_open() ||!src1.is_open() || !dest.is_open())

            return false; // could not be copied

     

        dest << src1.rdbuf (); // copy the content

          dest << src2.rdbuf ();

        dest.close (); // close destination file

        src1.close (); // close source file1

          src1.close (); // close source file2

        return true; // file copied successfully

    }

     

    int _tmain(int argc, _TCHAR* argv[])

    {

          if (!copyFile ("D:\\1.txt","D:\\2.txt", "D:\\a.txt"))

            std::cout << "File could not be copied successfully";

     

        else

            std::cout << "File copied successfully!";

     

        std::cin.get (); // pause for input

        return EXIT_SUCCESS; // program was executed successfully

    //return 0;

    }

     

    如果您的问题解决了,请把有用的回答标记为答案!

     

    谢谢,

    Lucy

     


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年5月3日 2:54
    版主

全部回复

  • 在您调用in.open("D:\\2.txt",ios::in);的前面加上in.clear();试试


    Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
    2011年4月28日 14:36
    版主
  • 1out<<ch; 改为out<<ch<<endl;

    2如果还不行,尝试使用wifstream和woifstream


    麻烦把正确答案设为解答。
    2011年4月29日 1:54
    版主
  • 吴岚,

    您好!如果您想实现D盘里的两个文件(1.txt2.txt)中的内容复制到D盘里的a.txt文档中。您可以使用以下代码。

    #include "stdafx.h"

    #include <fstream>

    #include <iostream>

     

    using namespace std;

     

    bool copyFile (const char SRC1[],const char SRC2[], const char DEST[])

    {

        std::ifstream src1; // the source file1

          std::ifstream src2; // the source file2

        std::ofstream dest; // the destination file

     

        src1.open (SRC1, std::ios::binary); // open in binary to prevent jargon at the end of the buffer

          src2.open (SRC2, std::ios::binary); // open in binary to prevent jargon at the end of the buffer

        dest.open (DEST, std::ios::binary); // same again, binary

        if (!src1.is_open() ||!src1.is_open() || !dest.is_open())

            return false; // could not be copied

     

        dest << src1.rdbuf (); // copy the content

          dest << src2.rdbuf ();

        dest.close (); // close destination file

        src1.close (); // close source file1

          src1.close (); // close source file2

        return true; // file copied successfully

    }

     

    int _tmain(int argc, _TCHAR* argv[])

    {

          if (!copyFile ("D:\\1.txt","D:\\2.txt", "D:\\a.txt"))

            std::cout << "File could not be copied successfully";

     

        else

            std::cout << "File copied successfully!";

     

        std::cin.get (); // pause for input

        return EXIT_SUCCESS; // program was executed successfully

    //return 0;

    }

     

    如果您的问题解决了,请把有用的回答标记为答案!

     

    谢谢,

    Lucy

     


    Lucy Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年5月3日 2:54
    版主