0

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;
}
newlearner
  • 149
  • 1
  • 11
  • 3
    Possible duplicate of [C++ Reading the Data part of a WAV file](https://stackoverflow.com/questions/13660777/c-reading-the-data-part-of-a-wav-file) – Matt Messersmith May 14 '19 at 17:09
  • 1
    Probably after fopen you should call fread. But also have a look into `std::vector`, you might find it useful. – rustyx May 14 '19 at 17:09
  • 4
    Yes, defining your own `Array` class is bananas, use `std::vector` – Matt Messersmith May 14 '19 at 17:10
  • 1
    Why not use `std::vector` rather than all that unpleasant manual memory management code? – Jesper Juhl May 14 '19 at 17:10
  • I've to do it with template that's why :D – newlearner May 14 '19 at 17:13
  • 5
    @newlearner `std::vector` is a templated class. It works correctly, while your home-made class has many faults. – PaulMcKenzie May 14 '19 at 17:14
  • 1
    Stop whatever you are doing and learn about std::vector. – n. m. could be an AI May 14 '19 at 17:15
  • okay thank you very much I'll have a look ! :) – newlearner May 14 '19 at 17:16
  • 1
    Also, learn about `std::ifstream` and `std::ofstream`, instead of using `fopen()` – Remy Lebeau May 14 '19 at 17:23
  • 2
    Eventually you probably will have to write something like this, so here's some recommended reading to help make that as smooth as possible: [What is meant by Resource Acquisition is Initialization (RAII)?](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) and [What is The Rule of Three?](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) – user4581301 May 14 '19 at 17:23
  • 1
    "I've to do it with template that's why :D " -- when you have a requirement to do something, I think it does not invalidate your effort to model your version after something that you know works. Consider 2 steps: a) Write code using std::vector. When that combo does what you want, b) substitute your "dynamic Array" for the std::vector, and re-run your tests. – 2785528 May 14 '19 at 21:13

2 Answers2

4

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>()};
}

... 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";
    }
}

... 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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
-2
char* readFileBytes(const char *name)  
{  
    FILE *fl = fopen(name, "r");  
    fseek(fl, 0, SEEK_END);  
    long len = ftell(fl);  
    char *ret = malloc(len);  
    fseek(fl, 0, SEEK_SET);  
    fread(ret, 1, len, fl);  
    fclose(fl);  
    return ret;  
}  
James LoForti
  • 1,456
  • 1
  • 8
  • 8
  • Missing a cast. `char *ret = malloc(len);` -> `char *ret = (char*)malloc(len);` C++ is quite a bit more anally retentive about the use of `void*` than C is. In C++ there is almost always a better alternative than `void*`, so anytime you find yourself making a cast like this, stop and ask yourself what you're doing wrong. – user4581301 May 14 '19 at 17:31
  • @user4581301 a C++ cast would be better than a C-style cast there. If you *insist* on `malloc`. C-style casts are nasty, since, in the worst case, they degenerate into `reinterpret_cast>`. You don't know what you get (well, you *do*, but you have to know the standard *really* well) and it's easy to make mistakes. The C++ casts are much more explicit (and `grep`able). – Jesper Juhl May 14 '19 at 18:38
  • I could argue that just for fun, but I'd be doing the truth a disservice. – user4581301 May 14 '19 at 18:57