c++ - Extra characters on cstring when cout -


i have char[4] datalabel when

wav.read(datalabel, sizeof(datalabel));//read data label cout << "data label:" <<datalabel <<  "\n"; 

i output data label:data� when loop through each char correct output, should "data".

for (int = 0; < sizeof(datalabel); ++i) {     cout << "data label " << << " " << datalabel[i] <<  "\n"; } 

the sizeof returns 4. i'm @ loss issue is.

edit: confuses me more same code earlier in program works perfectly.

ifstream wav; wav.open("../../desktop/hello.wav", ios::binary); char riff[4]; //char hold riff header  if (wav.is_open()) {     wav.read(riff, sizeof(riff));//read riff header     if ((strcmp(riff, "riff"))!=0) {          fprintf(stderr, "not wav file");         exit(1);     }     else {         cout << "riff:" << riff << "\n"; 

this prints riff:riff intended.

you missing null terminator on character array. try making 5 characters , making last character '\0'. lets program know string done without needing know size.

what null-terminated string?


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -