2014-07-18 100 views
-1

我的程序(見下文)創建一個帶有標題的wav文件等等。我可以打開它,但不會複製所有的數據塊。標題正常:我可以用WMP打開文件,我可以聽到一些噪音,但對於數據,它實際上並不複製所有文本。我用Wordpad打開了兩個wav文件,數據只收集前三行字符,然後是所有空格(所以數據大小很好)。複製wav文件C++

fstream ifs(FileInputPath->c_str(), ios_base::in); 
cout<< "PATH :" << FileInputPath->c_str()<<endl; 
ofstream outfile("C:/Users/miguel/Desktop/proj/Automatic_Visual_Speech_v0.9_beta/exemplo.wav", ofstream::out); 
if (ifs.is_open() && outfile.is_open()) 
{ 
    char First_Chunk_ID[5]=""; //RIFF 
    ifs.read(First_Chunk_ID,4); 
    outfile.write(First_Chunk_ID,4); 

    long File_Size; // FileSize 
    ifs.read(reinterpret_cast<char*>(&File_Size), sizeof(long)); 
    outfile.write(reinterpret_cast<char*>(&File_Size),sizeof(long)); 

    char Form_Type_ID[5] =""; //Formato 
    ifs.read(Form_Type_ID,4); 
    outfile.write(Form_Type_ID,4); 

    char Second_Chunk_ID[5] = ""; //2ºPedaco 
    ifs.read(Second_Chunk_ID,4); 
    outfile.write(Second_Chunk_ID,4); 

    /*char * charArray_Wave_Format_Size = new char; 
    ifs.read(charArray_Wave_Format_Size, sizeof(long));*/ 

    long Wave_Format_Size; //Tamanho do 2º Pedaço 
    ifs.read(reinterpret_cast<char*>(&Wave_Format_Size), sizeof(long)); 
    outfile.write(reinterpret_cast<char*>(&Wave_Format_Size), sizeof(long)); 

    char Wave_Format_Info[3] = ""; //Tipo de formato! 
    ifs.read(Wave_Format_Info, 2); 
    outfile.write(Wave_Format_Info,2); 

    short NumChannels; //Canais 
    ifs.read(reinterpret_cast<char*>(&NumChannels),2); 
    outfile.write(reinterpret_cast<char*>(&NumChannels),2); 

    long SampleRate; 
    ifs.read(reinterpret_cast<char*>(&SampleRate),4); 
    outfile.write(reinterpret_cast<char*>(&SampleRate),4); 

    long ByteRate; 
    ifs.read(reinterpret_cast<char*>(&ByteRate),4); 
    outfile.write(reinterpret_cast<char*>(&ByteRate),4); 

    short BlockAlign; 
    ifs.read(reinterpret_cast<char*>(&BlockAlign),2); 
    outfile.write(reinterpret_cast<char*>(&BlockAlign),2); 

    short BitsPerSample; 
    ifs.read(reinterpret_cast<char*>(&BitsPerSample),2); 
    outfile.write(reinterpret_cast<char*>(&BitsPerSample),2); 

    char Third_Chunk_ID[5] = ""; 
    ifs.read(Third_Chunk_ID, 4); 
    outfile.write(Third_Chunk_ID,4); 

    long charArray_Data_Size; 
    ifs.read(reinterpret_cast<char*>(&charArray_Data_Size), sizeof(long)); 
    outfile.write(reinterpret_cast<char*>(&charArray_Data_Size),sizeof(long)); 
      char Data[81600]="";// if you want to read 10000 chars, make a buffer of 10000 chars 
    ifs.read(Data,charArray_Data_Size+1); // use read(), not get(). Everything in the file is binary 
    outfile.write(Data,charArray_Data_Size+1); 
    outfile.close(); 
} 

回答

1

您不顯示如何打開輸出文件,因此我使用了一種心理猜測:您打開的文件不是二進制文本。

+0

對不起,我複製粘貼+我沒有露面的代碼,我不喜歡: \t fstream的IFS(FileInputPath-> c_str()的ios_base ::在); \t cout <<「PATH:」<< FileInputPath-> c_str()<< endl; \t ofstream outfile(PATH,ofstream :: out); – user3259516