Hi All. I am new to C++, infact I started watching tutorials just about an hour ago. But, I had some experiece of Javascript, PHP, so It is fine.

Now, my question is that, I am untimately, learning C++ to be able to manipulate a .wav files, just to write a script from scratch and be able to read file, and illustrate in a graphical interface like a spectrogram.

But, since I am a newbie, I decided to start learning about reading binary files first, and actually used a code from this website http://www.cplusplus.com/reference/istream/istream/read/ to read a simple [code]test.txt[/code] file, which I wrote my name in, and runned the code

// read a file into memory
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

int main () {

  std::ifstream is ("test.txt", std::ifstream::binary);
  if (is) {
    // get length of file:
    is.seekg (0, is.end);
    int length = is.tellg();
    is.seekg (0, is.beg);

    char * buffer = new char [length];

    std::cout << "Reading " << length << " characters... ";
    // read data as a block:
    is.read (buffer,length);

    if (is)
      std::cout << "all characters read successfully.";
    else
      std::cout << "error: only " << is.gcount() << " could be read";
    is.close();

    // ...buffer contains the entire file...

    delete[] buffer;
  }
  return 0;
}

and It gave me, a message saying [code]Reading 32 characters... all characters read successfully.
[/code] even though, I only had like 29 characters in there. Anyway, that is not the problem.

The problem, is that, I placed a .wav file at the place where it said test.txt and even placed the .wav file in the same directory, and when I ran the code, it does not give me error or success, it shell just says, "Process returned 0 (0x0) execution time 0.008s"

What am I doing wrong? I just want to read the contents of what is in that wav file only? Be it in a frequency/amplitude/sound wave whatever the term is. Why is it not show

Hello,

If you're reading a .wav file then you need to read the file properly; the .wav file contains both the "Header" information, as well as the actual "raw data" and you need to read these separately rather than the whole of the file.

You can check this website for more details on how to read a .wav file:

Click Here

This could be the problem that you're having when reading in the .wav file since you do not consider these values. From my experience, and, IMO you cannot just read a single .wav file into one massive chunk. For example, the "raw data" which I'm sure is what you want to manipulate contains both raw (native) as well as (double) values and thus depends on the bit-rate of the actual file.

I wrote a class for my finals which reads in both the raw data, as well as the header data. Message back if you would like to take a look at it, feel free to!

P.S. If you're attempting to do I am untimately, learning C++ to be able to manipulate a .wav files, just to write a script from scratch and be able to read file, and illustrate in a graphical interface like a spectrogram. then you should really consider using libraries, for example, something like this may involve the use of an FFT in order to convert the signal from the time domain into the freq domain which may involve some pretty intensive programming and mathematical skills.

Hope this helps

But, since I am a newbie, I decided to start learning about reading binary files first

Yes. Learn by taking one small step at a time.

And while you are learning, learn good idioms. This is rarely right: char * buffer = new char [length];
Instead, use std::vector<>
or if you must, use a smart pointer - for instance:
std::unique_ptr< char[] > buffer( new char [length] ) ;

#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>

using byte = unsigned char ;

std::vector<byte> read_bytes( const std::string& path_to_file )
{
    std::ifstream file( path_to_file, std::ios::binary ) ;
    return { std::istream_iterator<byte>(file), std::istream_iterator<byte>() } ;
}

int main()
{
    std::cout << "dump of bytes in this file:\n" << std::hex ;
    for( byte b : read_bytes( __FILE__ ) ) std::cout << int(b) << ' ' ;
    std::cout << '\n' ;
}

Or, if the compiler is an old one (C++98):

#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>

typedef unsigned char byte ;

std::vector<byte> read_bytes( const char* path_to_file )
{
    std::ifstream file( path_to_file, std::ios::binary ) ;
    return std::vector<byte>( std::istream_iterator<byte>(file),
                              std::istream_iterator<byte>() ) ;
}

int main()
{
    std::vector<byte> bytes = read_bytes( __FILE__ ) ;
    std::cout << "dump of bytes in this file:\n" << std::hex ;
    for( std::size_t i = 0 ; i < bytes.size() ; ++i  ) std::cout << int( bytes[i] ) << ' ' ;
    std::cout << '\n' ;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.