c++ - printf用%s輸出長度為4的字元數組,但是實際輸出位數不為4
某草草
某草草 2017-06-12 09:24:55
0
1
1212

#如圖所示,本來是長度為4的字元數組,用%c手動一個一個輸出是正常顯示,但是用%s直接輸出這個字元數組就出現輸出位數並不是4,這是什麼原因呢?是這個字元數組的後面沒有帶0嗎?遇到這種情況有什麼簡單的方法快速輸出字串嗎? (萬一有個長度幾百的字元陣列要輸出難道我得手動一個一個用字元陣列成員拼接嗎?)

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct WAV_header {
    char ChunkID[4];   /* "RIFF" */
    uint32_t ChunkSize; /* 36 + Subchunk2Size */
    char Format[4];    /* "WAVE" */

    /* sub-chunk "fmt" */
    char Subchunk1ID[4];   /* "fmt " */
    uint32_t Subchunk1Size; /* 16 for PCM */
    uint16_t AudioFormat;   /* PCM = 1*/
    uint16_t NumChannels;   /* Mono = 1, Stereo = 2, etc. */
    uint32_t SampleRate;    /* 8000, 44100, etc. */
    uint32_t ByteRate;  /* = SampleRate * NumChannels * BitsPerSample/8 */
    uint16_t BlockAlign;    /* = NumChannels * BitsPerSample/8 */
    uint16_t BitsPerSample; /* 8bits, 16bits, etc. */

    /* sub-chunk "data" */
    char Subchunk2ID[4];   /* "data" */
    uint32_t Subchunk2Size; /* data size */
} Wav_header_t;

typedef struct WAV_block_header {
    char blockID[4];   /* "data" */
    uint32_t blockSize; /* data size */
} Wav_block_header_t;


int main()
{
    FILE *fp = NULL;
    Wav_header_t header;
    Wav_block_header_t block_header;

    fp = fopen("test.wav", "rb");
    if (!fp) {
        printf("can't open audio file\n");
        exit(1);
    }

    fread(&header, 1, sizeof(Wav_header_t), fp);

//    printf("length:%d(10),0x%x, \n\n", sizeof(struct WAV_Format), sizeof(struct WAV_Format));  // 44
    printf("ChunkID \t%c%c%c%c\n", header.ChunkID[0], header.ChunkID[1], header.ChunkID[2], header.ChunkID[3]);
    printf("ChunkSize \t%d\n", header.ChunkSize);
    printf("Format \t%s\n", header.Format);
    printf("Subchunk1ID \t%x\n", header.Subchunk1ID);
    printf("Subchunk1Size \t%d\n", header.Subchunk1Size);
    printf("AudioFormat \t%d\n", header.AudioFormat);
    printf("NumChannels \t%d\n", header.NumChannels);
    printf("SampleRate \t%d\n", header.SampleRate);
    printf("ByteRate \t%d\n", header.ByteRate);
    printf("BlockAlign \t%d\n", header.BlockAlign);
    printf("BitsPerSample \t%d\n", header.BitsPerSample);
    printf("Subchunk2ID \t%s\n", header.Subchunk2ID);
    printf("Subchunk2Size \t%d\n", header.Subchunk2Size);

    printf("\n");
    
    fread(&block_header, 1, sizeof(Wav_block_header_t), fp);
    printf("blockID \t%s\n", block_header.blockID);
    printf("blockSize \t%d\n", block_header.blockSize);
}
某草草
某草草

全部回覆(1)
学霸

C語言的字串結尾結束用'

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板