#include<iostream>
#include<fstream>
#include<iterator>
#include<string>
#include<conio.h>using namespace std;void main()
{
char data;
int a_file_size=0,b_file_size=0; fstream a_file;
fstream b_file; a_file.open("input.txt");
b_file.open("output.txt"); if(!a_file||!b_file)
{
cerr<<"!!unable to open the necessary files.\n";
return;
} /*while(a_file.get(data))
a_file_size ++;
while(b_file.get(data))
b_file_size ++;  cout<<a_file_size<<" "<<b_file_size<<endl;*/

while(a_file.get(data))
{
cout<<data;
b_file.put(data);
}

_getch();  
}

注释中的语句一旦执行,文件就会不能拷贝,我想是不是前两个循环把文件指针移动到了文件尾部,所以while判断时就推出来了。请问用什么语句可以移动文件指针到头部?

解决方案 »

  1.   

    use
    seekg(0, std::ios::beg);
    but you should be able to do something likefstream a_file;
    a_file.open("input.txt");
    a_file.seekg(0, std::ios::end);
    int length = a_file.tellg();
      

  2.   

    also, you could be using copy algorithm, something likestd::ifstream a_file("input.text", std::ios_base::binary);
    std::ofstream b_file("output.txt, std::ios_base::binary);std::copy(std::istreambuf_iterator<char>(a_file),
    std::istreambuf_iterator<char>(),
    std::ostreambuf_iterator<char>(b_file));or
    b_file << a_file.rdbuf();
      

  3.   

    sorry, should be
    std::ifstream a_file("input.text", std::ios_base::binary);
    std::ofstream b_file("output.txt", std::ios_base::binary);std::copy(std::istreambuf_iterator<char>(a_file),
    std::istreambuf_iterator<char>(),
    std::ostreambuf_iterator<char>(b_file));or
    b_file << a_file.rdbuf();