I have a char[4] dataLabel that when I say
wav.read(dataLabel, sizeof(dataLabel));//Read data label cout << "Data label:" <<dataLabel << "\n"; I get the output Data label:data� but when I loop through each char I get the correct output, which should be "data".
for (int i = 0; i < sizeof(dataLabel); ++i) { cout << "Data label " << i << " " << dataLabel[i] << "\n"; } The sizeof returns 4. I'm at a loss for what the issue is.
EDIT: What confuses me more is that essentially the same code from earlier in my program works perfectly.
ifstream wav; wav.open("../../Desktop/hello.wav", ios::binary); char riff[4]; //Char to hold RIFF header if (wav.is_open()) { wav.read(riff, sizeof(riff));//Read RIFF header if ((strcmp(riff, "RIFF"))!=0) { fprintf(stderr, "Not a wav file"); exit(1); } else { cout << "RIFF:" << riff << "\n"; This prints RIFF:RIFF as intended.
char[4] dataLabel), and the data it holds is 4 characters long ("data"). It is definitely missing the null character.char[5] dataLabel. If"data"is being read in, there's no space for the\0character!