Reading and manipulating a WAV file in C++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to write a simple program to read a WAV file in C++ and get data like sample rate and bit depth. At some point I want to use this to manipulate the data to create an audio plugin, but I want to understand this first.
#include <iostream>
#include <string>
#include <fstream>
#include <ostream>
using namespace std;
struct wavfile {
char id[4];
int totallength;
char wavefmt[8];
int format;
short pcm;
short channels;
int frequency;
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
char data[4];
int bytes_in_data;
};
int main () {
FILE * wfile;
std::ofstream ofs;
wfile = ofs.open("tes.wav",std::ofstream::binary | std::ofstream::app); // line 31
ofs.close();
return 0;
}
When I try to run it it keep saying "assigning to 'FILE *' (aka '__sFILE *') from incompatible type 'void'"
How can I get it to open the wave file, read the data, so I can define it as a within the struct wavfile and print it back out?
Any help would be greatly appreciated, even just pointing me in the right direction or to a good resource. Thanks in advance!
c++ audio
add a comment |
I'm trying to write a simple program to read a WAV file in C++ and get data like sample rate and bit depth. At some point I want to use this to manipulate the data to create an audio plugin, but I want to understand this first.
#include <iostream>
#include <string>
#include <fstream>
#include <ostream>
using namespace std;
struct wavfile {
char id[4];
int totallength;
char wavefmt[8];
int format;
short pcm;
short channels;
int frequency;
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
char data[4];
int bytes_in_data;
};
int main () {
FILE * wfile;
std::ofstream ofs;
wfile = ofs.open("tes.wav",std::ofstream::binary | std::ofstream::app); // line 31
ofs.close();
return 0;
}
When I try to run it it keep saying "assigning to 'FILE *' (aka '__sFILE *') from incompatible type 'void'"
How can I get it to open the wave file, read the data, so I can define it as a within the struct wavfile and print it back out?
Any help would be greatly appreciated, even just pointing me in the right direction or to a good resource. Thanks in advance!
c++ audio
Hello,std::ofstream::openreturns void so you cannot set the value of a variable using its return value. More overstd::ofsteamis an out file stream so you cannot read things from it. I think you're looking forstd::ifstream(cplusplus.com/reference/istream/istream/read). And you don't need one of your variable, why do you have aFILE*and anstd::ofstream? If you want to read your file, either usestd::ifstreamwhich belongs the C++ standard library or useFILEwhich belongs to C library but not both in the same time.
– Siliace
Nov 16 '18 at 23:14
What manual are you working from?
– Galik
Nov 16 '18 at 23:17
add a comment |
I'm trying to write a simple program to read a WAV file in C++ and get data like sample rate and bit depth. At some point I want to use this to manipulate the data to create an audio plugin, but I want to understand this first.
#include <iostream>
#include <string>
#include <fstream>
#include <ostream>
using namespace std;
struct wavfile {
char id[4];
int totallength;
char wavefmt[8];
int format;
short pcm;
short channels;
int frequency;
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
char data[4];
int bytes_in_data;
};
int main () {
FILE * wfile;
std::ofstream ofs;
wfile = ofs.open("tes.wav",std::ofstream::binary | std::ofstream::app); // line 31
ofs.close();
return 0;
}
When I try to run it it keep saying "assigning to 'FILE *' (aka '__sFILE *') from incompatible type 'void'"
How can I get it to open the wave file, read the data, so I can define it as a within the struct wavfile and print it back out?
Any help would be greatly appreciated, even just pointing me in the right direction or to a good resource. Thanks in advance!
c++ audio
I'm trying to write a simple program to read a WAV file in C++ and get data like sample rate and bit depth. At some point I want to use this to manipulate the data to create an audio plugin, but I want to understand this first.
#include <iostream>
#include <string>
#include <fstream>
#include <ostream>
using namespace std;
struct wavfile {
char id[4];
int totallength;
char wavefmt[8];
int format;
short pcm;
short channels;
int frequency;
int bytes_per_second;
short bytes_by_capture;
short bits_per_sample;
char data[4];
int bytes_in_data;
};
int main () {
FILE * wfile;
std::ofstream ofs;
wfile = ofs.open("tes.wav",std::ofstream::binary | std::ofstream::app); // line 31
ofs.close();
return 0;
}
When I try to run it it keep saying "assigning to 'FILE *' (aka '__sFILE *') from incompatible type 'void'"
How can I get it to open the wave file, read the data, so I can define it as a within the struct wavfile and print it back out?
Any help would be greatly appreciated, even just pointing me in the right direction or to a good resource. Thanks in advance!
c++ audio
c++ audio
edited Nov 16 '18 at 23:12
Jon Organ
asked Nov 16 '18 at 22:59
Jon OrganJon Organ
11
11
Hello,std::ofstream::openreturns void so you cannot set the value of a variable using its return value. More overstd::ofsteamis an out file stream so you cannot read things from it. I think you're looking forstd::ifstream(cplusplus.com/reference/istream/istream/read). And you don't need one of your variable, why do you have aFILE*and anstd::ofstream? If you want to read your file, either usestd::ifstreamwhich belongs the C++ standard library or useFILEwhich belongs to C library but not both in the same time.
– Siliace
Nov 16 '18 at 23:14
What manual are you working from?
– Galik
Nov 16 '18 at 23:17
add a comment |
Hello,std::ofstream::openreturns void so you cannot set the value of a variable using its return value. More overstd::ofsteamis an out file stream so you cannot read things from it. I think you're looking forstd::ifstream(cplusplus.com/reference/istream/istream/read). And you don't need one of your variable, why do you have aFILE*and anstd::ofstream? If you want to read your file, either usestd::ifstreamwhich belongs the C++ standard library or useFILEwhich belongs to C library but not both in the same time.
– Siliace
Nov 16 '18 at 23:14
What manual are you working from?
– Galik
Nov 16 '18 at 23:17
Hello,
std::ofstream::open returns void so you cannot set the value of a variable using its return value. More over std::ofsteam is an out file stream so you cannot read things from it. I think you're looking for std::ifstream (cplusplus.com/reference/istream/istream/read). And you don't need one of your variable, why do you have a FILE* and an std::ofstream? If you want to read your file, either use std::ifstream which belongs the C++ standard library or use FILE which belongs to C library but not both in the same time.– Siliace
Nov 16 '18 at 23:14
Hello,
std::ofstream::open returns void so you cannot set the value of a variable using its return value. More over std::ofsteam is an out file stream so you cannot read things from it. I think you're looking for std::ifstream (cplusplus.com/reference/istream/istream/read). And you don't need one of your variable, why do you have a FILE* and an std::ofstream? If you want to read your file, either use std::ifstream which belongs the C++ standard library or use FILE which belongs to C library but not both in the same time.– Siliace
Nov 16 '18 at 23:14
What manual are you working from?
– Galik
Nov 16 '18 at 23:17
What manual are you working from?
– Galik
Nov 16 '18 at 23:17
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346491%2freading-and-manipulating-a-wav-file-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53346491%2freading-and-manipulating-a-wav-file-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hello,
std::ofstream::openreturns void so you cannot set the value of a variable using its return value. More overstd::ofsteamis an out file stream so you cannot read things from it. I think you're looking forstd::ifstream(cplusplus.com/reference/istream/istream/read). And you don't need one of your variable, why do you have aFILE*and anstd::ofstream? If you want to read your file, either usestd::ifstreamwhich belongs the C++ standard library or useFILEwhich belongs to C library but not both in the same time.– Siliace
Nov 16 '18 at 23:14
What manual are you working from?
– Galik
Nov 16 '18 at 23:17