如何在C ++中将Wav文件加载到数组中? [英] How to load a Wav file in an Array in C++?

查看:146
本文介绍了如何在C ++中将Wav文件加载到数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hey I've a dynamic array and I want to load to this array the data of my Wav file, I already wrote the beginning but I can't figure it out how to load the file in my dynamic array, can somebody help me further with this code?

#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;
}


推荐答案

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

if you just want to load the complete file into memory, this may come in handy:

#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 ++文件流类来打开和自动关闭文件

... and use the C++ file streaming classes to open and automatically close files.

{
    // 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文件包含很多不同的块描述了文件的内容,因此您可能需要创建单独的类,以将这些块与文件进行流传输。

... but a WAV file contains a lot of different chunks describing the contents of the file so you may want to create individual classes for streaming these chunks to and from files.

这篇关于如何在C ++中将Wav文件加载到数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆