cout时cstring上的额外字符

时间:2015-07-07 20:31:47

标签: c++

当我说

时,我有一个char[4] dataLabel
wav.read(dataLabel, sizeof(dataLabel));//Read data label
cout << "Data label:" <<dataLabel <<  "\n";

我得到输出Data label:data�但是当我遍历每个char时,我得到正确的输出,应该是“data”。

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

sizeof返回4.我对此问题感到茫然。

编辑:让我更加困惑的是,我程序中早期基本相同的代码完美无缺。

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

按预期打印RIFF:RIFF

5 个答案:

答案 0 :(得分:3)

您在字符数组上缺少空终止符。尝试将其设为5个字符,并将最后一个字符设为'\ 0'。这使程序知道你的字符串完成而无需知道大小。

What is a null-terminated string?

答案 1 :(得分:1)

operator<< std::ostreamchar const* expects a null terminated string的超载string。你给它一个4个字符的数组。

改为使用标准库std::string dataLabel; 类:

An instance 0x6080001250a0 of class LineGraphic was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x618000043ea0> (
<NSKeyValueObservance 0x6180000c1ce0: Observer: 0x6000001834d0, Key path: points, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x600000056fe0>
<NSKeyValueObservance 0x6180000c1e30: Observer: 0x6000001834d0, Key path: shadow, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x600000057460>
<NSKeyValueObservance 0x6180000c9bc0: Observer: 0x6000001834d0, Key path: secondHeadStyle, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60000005d7c0>
<NSKeyValueObservance 0x6180000c1ff0: Observer: 0x6000001834d0, Key path: lineStyle, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x600000051640>
<NSKeyValueObservance 0x6180000c1f10: Observer: 0x6000001834d0, Key path: fillColor, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x600000054f70>
<NSKeyValueObservance 0x6180000c1b20: Observer: 0x6000001834d0, Key path: strokeWidth, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x600000054d30>
<NSKeyValueObservance 0x6180000c2140: Observer: 0x6000001834d0, Key path: strokeColor, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x6000000548b0>
<NSKeyValueObservance 0x6180000c1d50: Observer: 0x6000001834d0, Key path: firstHeadStyle, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x600000055060>
<NSKeyValueObservance 0x6180000c1ea0: Observer: 0x6000001834d0, Key path: opacity, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x6080002421f0>
<NSKeyValueObservance 0x6180000c1f80: Observer: 0x6000001834d0, Key path: visibleOpacity, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x608000247560>
<NSKeyValueObservance 0x6180000ca090: Observer: 0x6000001834d0, Key path: rotation, Options: <New: NO, Old: YES, Prior: YES> Context: 0x0, Property: 0x60800024a590>
)

答案 2 :(得分:0)

the documentation for istream::read;它不附加一个空终止符,并且你告诉它只读4个字符。正如其他人所指出的那样,<<运算符正在寻找一个空终止符,因此它会继续读取数组的末尾,直到找到它为止。

我同意使用std::string代替char[]的其他建议答案。

答案 3 :(得分:0)

您的char[]数组不以空值终止,但接受<<输入的char*运算符需要空终止符。

char dataLabel[5];
wav.read(dataLabel, 4); //Read data label
dataLabel[4] = 0;
cout << "Data label:" << dataLabel <<  "\n";

答案 4 :(得分:0)

变量dataLabel定义为

char[4] dataLabel;

它只有四个字符填充了字符{&#39; d&#39;&#39; a&#39;,&#39;&#39;&#39; a&#39; )声明

wav.read(dataLabel, sizeof(dataLabel));//

因此,当它的参数是字符数组时,此字符数组不具有operator <<所需的终止零。

因此在本声明中

cout << "Data label:" <<dataLabel <<  "\n";

程序有未定义的行为。

将其更改为

std::cout << "Data label: ";
std::cout.write( dataLabel, sizeof( dataLabel ) )  <<  "\n";