读取记录的文件时出现问题. [英] Problem reading a recorded file.
问题描述
您好,我正在做一个录音应用程序.我正在将音频数据写入文件.我正在尝试读取文件中的数据并将其添加到缓冲区.在调试过程中,我发现错误指向:
Hello, I am doing an audio recording application. I am writing the audio data to a file. I am trying to read the data in the file and add it to a buffer. During debugging, I find the error is pointing to:
ASSERT(AfxIsValidAddress(lpBuf, nCount));
功能的
CFile::Read
.这是代码:
function. Here is the code:
void CWaveIn::WriteWaveFile()
{
CString* databuf; //buffer to hold the data from the file.
databuf = NULL;
databuf = new CString;
CFile my_file,f1;
if(my_file.Open(m_strFile,CFile::modeRead|CFile::shareDenyNone,NULL))
{
unsigned int x = ;
int iRead = my_file.Read((void*)databuf, my_file.GetLength());//error here
.....
如果您认为我的录音有问题,也要粘贴该代码:
(我正在使用回调线程进行记录.该线程的功能如下所示)
If you think, I have problem with my recording, I am pasting that code as well:
(I am using a callback thread, for recording. The thread''s function is written below)
DWORD WINAPI CWaveIn::ThreadProc(LPVOID lpParameter)
{
CWaveIn* pWaveIn = (CWaveIn*)lpParameter;
MSG msg;
DWORD dwWait = 0;
DWORD dwUser = 0;
WAVEHDR *pWaveHdr = NULL;
if (pWaveIn == NULL)
{
return -1;
}
while ((dwWait = WaitForSingleObject(pWaveIn->m_hEventKill, 5)) != 0)
{
// Handle MMAPI wave capturing thread messages
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
switch (msg.message)
{
case MM_WIM_DATA:
EnterCriticalSection(&pWaveIn->m_critSecRtp);
pWaveHdr = (WAVEHDR*)msg.lParam;
// Unprepare header, initialize with the next buffer position, prepare the header
// again and add it to the wave capture device.
if (pWaveHdr)
{
waveInUnprepareHeader(pWaveIn->m_hWaveIn, pWaveHdr, sizeof(WAVEHDR));
dwUser = pWaveHdr->dwUser;
//m_fRecord is a FILE* in CWaveIn class
fwrite(pWaveHdr->lpData, 1, pWaveHdr->dwBytesRecorded, pWaveIn->m_fRecord);
ZeroMemory(pWaveHdr, sizeof(WAVEHDR));
pWaveHdr->dwBufferLength = BYTES_PER_HDR;
pWaveHdr->lpData = (char*)pWaveIn->m_HdrBuffer[dwUser];
pWaveHdr->dwUser = dwUser;
waveInPrepareHeader(pWaveIn->m_hWaveIn, pWaveHdr, sizeof(WAVEHDR));
waveInAddBuffer(pWaveIn->m_hWaveIn, pWaveHdr, sizeof(WAVEHDR));
}
LeaveCriticalSection(&pWaveIn->m_critSecRtp);
break;
default:
break;
}
}
}
CloseHandle(pWaveIn->m_thread);
pWaveIn->m_thread = NULL;
return 0;
}
推荐答案
为什么要为缓冲区分配一个新的空CString,然后再读取它?
Why are you assigning a new, empty CString to the buffer and then reading into it?
CString* databuf; //buffer to hold the data from the file.
databuf = NULL;
databuf = new CString;
...
int iRead = my_file.Read((void*)databuf, my_file.GetLength());//error here
当然,分配一个足以容纳文件中数据的缓冲区是更好的主意吗?空的CString不会神奇地"扩展自身以处理文件数据,仅仅是因为您已经分配了它并将其强制转换为空指针!看一下CFile.Read函数,然后分配一个缓冲区: http ://msdn.microsoft.com/zh-CN/library/ctka0kks(v = vs.80).aspx [
Surely, assigning a buffer that was big enough to hold the data in the file would be a better idea? An empty CString is not going to "magically" expand itself to cope with the file data just because you have assigned it and cast it to a pointer-to-void! Have a look at the CFile.Read function, and allocate a buffer instead: http://msdn.microsoft.com/en-us/library/ctka0kks(v=vs.80).aspx[^]
您不正确地使用了CString
它不代表带有缓冲区的对象,您可以将其涂写到...
更改为
char *databuf=new char[my_file.GetLength()];
在.Open
子句中
并确保在出门时将其删除
you''re usingCString
incorrectly
It does not represent an object with a buffer that you can scribble into ...
change it to
char *databuf=new char[my_file.GetLength()];
inside the.Open
clause
and make sure you delete it on the way out
这篇关于读取记录的文件时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!