如何在C ++中将Wav文件加载到数组中?

时间:2019-05-14 17:00:21

标签: c++ arrays templates wav

嘿,我有一个动态数组,我想将Wav文件的数据加载到该数组中,我已经写了开头,但是我不知道如何在动态数组中加载文件,有人

#include <iostream> 
using namespace std;

template <typename T> 
class Array{
public:
    int size;
    T *arr;

    Array(int s){
    size = s;
    arr = new T[size];
    }

    T& operator[](int index)
    {
        if (index > size)
            resize(index);
        return arr[index];
    }

 void resize(int newSize) { 
        T* newArray = new T[newSize];
        for (int i = 0; i <size; i++)
        {
            newArrayi] = arr[i];
        }
        delete[] arr;
        arr = newArray;
        size = newSize;
    }
};
int main(){

    Array<char> wavArray(10);
    FILE  *inputFile;
    inputFile =fopen("song.wav", "rb");

        return 0;
}

2 个答案:

答案 0 :(得分:3)

如果您只想将整个文件加载到内存中,这可能会派上用场:

#include <iterator>

// a function to load everything from an istream into a std::vector<char>
std::vector<char> load_from_stream(std::istream& is) {
    return {std::istreambuf_iterator<char>(is), std::istreambuf_iterator<char>()};
}

...并使用C ++文件流类打开和自动关闭文件。

{
    // open the file
    std::ifstream is(file, std::ios::binary);

    // check if it's opened
    if(is) {
        // call the function to load all from the stream
        auto content = load_from_stream(is);

        // print what we got (works on textfiles)
        std::copy(content.begin(), content.end(),
                  std::ostream_iterator<char>(std::cout));
    } else {
        std::cerr << "failed opening " << file << "\n";
    }
}

...但是WAV文件包含许多描述文件内容的不同块,因此您可能需要创建单独的类以将这些块与文件进行流传输。

答案 1 :(得分:-2)

Exception in thread "main" java.lang.NullPointerException
    at java.applet.Applet.getCodeBase(Unknown Source)
    at GameAudio.init(GameAudio.java:9)
    at MathGameApp.main(MathGameApp.java:9)