简体   繁体   English

c-从char读取单个字符

[英]c - Reading individual characters from a char

I am working on a project using DirectSound and am trying to make it so that the header information for the sound file is passed to the processing method. 我正在使用DirectSound进行项目,并且正在尝试使之成为声音文件的标题信息传递到处理方法。 WAV files have a RIFF chunkID at the start. WAV文件的开头具有RIFF chunkID。 In a call to the ProcessWaveFile method, I pass the filepath as well as the header information. 在对ProcessWaveFile方法的调用中,我传递了文件路径以及标头信息。 The 'RIFF' chunkID is stored in a char. “ RIFF” chunkID存储在一个char中。 I need to be able to check individual characters of the chunkID against the actual file chunkID to ensure the file is correct. 我需要能够对照实际文件chunkID检查chunkID的各个字符,以确保文件正确。 This is a snippet of my code: 这是我的代码的片段:

    if((waveFileHeader.chunkId[0] != chunkID[0]) || (waveFileHeader.chunkId[1] != chunkID[1]) ||
    (waveFileHeader.chunkId[2] != chunkID[2]) || (waveFileHeader.chunkId[3] != chunkID[3]))
{
    MessageBox(hwnd, L"ChunkID not in the right Format.", L"Error", MB_OK);
    return false;
}

chunkId is the file's actual ID whereas chunkID is the ID passed through the function to check. chunkId是文件的实际ID,而chunkID是通过函数进行检查的ID。 As you can see I'm trying to handle it like an array here. 如您所见,我试图在这里像数组一样处理它。 The chunkId is stored in a char[]. chunkId存储在char []中。 Should I store the chunkID in an array too? 我是否也应该将chunkID存储在数组中? How would I specify it? 我要如何指定呢?

    bool ProcessWaveFile(char*, IDirectSoundBuffer8**, HWND, int, char, char, char, char, std::string, int, int);

Above is the header file line for the ProcessWaveFile method. 上面是ProcessWaveFile方法的头文件行。 The chunkID is specified as one of the chars. chunkID被指定为字符之一。 I could change it to char[] but the difficulty comes when actually calling the method. 我可以将其更改为char [],但实际调用该方法时会遇到困难。 Here is an example call: 这是一个示例调用:

    result1 = ProcessWaveFile("../terrain_sky/data/sound01.wav", &m_secondaryBuffer1, hwnd,
    44100, 'RIFF', 'fmt ', 'WAVE', 'data', "WAVE_PCM_FORMAT", 16, 2);

How could I declare the array values containg RIFF in this call without disrupting the chain of variables? 我如何在不中断变量链的情况下在此调用中声明包含RIFF的数组值?

If you are looking for a convenient way to check that the file starts with 'RIFF', there may be a more convenient way for you to find out. 如果您正在寻找一种方便的方法来检查文件是否以“ RIFF”开头,那么可能会有一种更方便的方法来查找。 Since the ChunkID is 4 bytes long, you may want to store what you expect 'RIFF' in a datatype that is also 4 bytes long (instead of a char array of length 4), then parse the first four bytes as, for example, an int. 由于ChunkID的长度为4个字节,因此您可能希望将期望的“ RIFF”存储在也是4个字节(而不是长度为4的char数组)的数据类型中,然后将前四个字节解析为例如一个int。

I have a wav file that starts with RIFF here, here's what number you get when you interpret the first four bytes as an int. 我这里有一个以RIFF开头的wav文件,这是将前四个字节解释为int时得到的数字。

od -ci -N4 ./test.wav
0000000  R   I   F   F
            1179011410

So the option -ci first prints each byte as its ASCII equivalent (RIFF), and then prints each set of 4 bytes as an integer. 因此,选项-ci首先将每个字节打印为等效的ASCII(RIFF),然后将每组4个字节打印为整数。 So really, 1179011410 = 'RIFF' and the integer may be easier for you to check for. 因此,实际上1179011410 ='RIFF',该整数可能更便于您检查。

But as an aside, I'm not really sure why you need to pass 'RIFF' into the ProcessWaveFile() function. 但顺便说一句,我不太确定为什么需要将'RIFF'传递到ProcessWaveFile()函数中。 If you only ever deal with RIFF files, you might get away with having that information stored in the ProcessWaveFile() function itself. 如果您只处理RIFF文件,则可以将这些信息存储在ProcessWaveFile()函数本身中,从而摆脱困境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM