2

我的函数是copy2,我在cplusplus.com上找到了函数copy1,我使用copy2有问题,但是copy 1没问题,copy2导致丢失关于atist,title,...的信息用于测试的文件mp3,我可以不明白为什么?

 #include <fstream>
 #include <iostream>
using namespace std;

void copy1()
{
    char * buffer;
    long size;

    ifstream infile ("Test.mp3",ifstream::binary);
    ofstream outfile ("Test2.mp3",ofstream::binary);

    // get size of file
    infile.seekg(0,ifstream::end);
    size=infile.tellg();
    infile.seekg(0);

    // allocate memory for file content
    buffer = new char [size];

    // read content of infile
    infile.read (buffer,size);

    // write to outfile
    outfile.write (buffer,size);

    // release dynamically-allocated memory
    delete[] buffer;
    cout<<"xong";
    outfile.close();
    infile.close();
    return ;
}
void copy2(){
    ifstream infile ("Test.mp3",ios::binary);
    ofstream outfile ("Test1.mp3",ios::binary);
    char c;
    while(!infile.eof())
    {
    infile.get(c);
    outfile.put(c);
    }
}
int main () {
    copy2();
}
4

1 回答 1

0

while(!infile.eof())可能会造成麻烦,因为它可能会在某些条件下返回错误的结果。我有过类似的问题。尝试使用这样的东西:

while (getline(infile,data))                                           
     {                                        
          cout<<data<<endl;
     }
于 2012-12-27T16:38:46.587 回答