CFile Error When Writing
-
Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception
An unknown error occurred while accessing file ..... file path + name ....
I am using following line code to open this file and write contents into itCFile* pFile = new CFile;
pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
pFile->Write((void*)pData, dwLength); // exception raised here
pFile->Close();What could be wrong here? Thanks for any input.
This world is going to explode due to international politics, SOON.
-
Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception
An unknown error occurred while accessing file ..... file path + name ....
I am using following line code to open this file and write contents into itCFile* pFile = new CFile;
pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
pFile->Write((void*)pData, dwLength); // exception raised here
pFile->Close();What could be wrong here? Thanks for any input.
This world is going to explode due to international politics, SOON.
-
Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception
An unknown error occurred while accessing file ..... file path + name ....
I am using following line code to open this file and write contents into itCFile* pFile = new CFile;
pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
pFile->Write((void*)pData, dwLength); // exception raised here
pFile->Close();What could be wrong here? Thanks for any input.
This world is going to explode due to international politics, SOON.
Since the exception is "An unknown error occurred while accessing file ", Do you still get errors with the unicode syntax _T( ) , e.g CFile file (_T ("File.txt"), CFile::modeReadWrite) );
CFile file;
CFileException e;
if (file.Open (_T ("File.txt"), CFile::modeReadWrite, &e)) {
// It worked!}
else {
// Open failed. Tell the user why.
e.ReportError ();
} -
Since the exception is "An unknown error occurred while accessing file ", Do you still get errors with the unicode syntax _T( ) , e.g CFile file (_T ("File.txt"), CFile::modeReadWrite) );
CFile file;
CFileException e;
if (file.Open (_T ("File.txt"), CFile::modeReadWrite, &e)) {
// It worked!}
else {
// Open failed. Tell the user why.
e.ReportError ();
}The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.
This world is going to explode due to international politics, SOON.
-
Why didn't you check
Open
return value (see, for instance, the documentation[^])?Veni, vidi, vici.
File opens successfully. It is the
Write()
function that is raising this exception.This world is going to explode due to international politics, SOON.
-
The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.
This world is going to explode due to international politics, SOON.
The error should be obvious, the inners of write look like this..
virtual void Write(
const void* lpBuf,
UINT nCount
);Parameters lpBuf - A pointer to the user-supplied buffer that contains the data to be written to the file. nCount - The number of bytes to be transferred from the buffer. For text-mode files, carriage return–linefeed pairs are counted as single characters. Official MSDN example:
CFile cfile;
cfile.Open(_T("Write_File.dat"), CFile::modeCreate | CFile::modeReadWrite);char pbufWrite[100];
memset(pbufWrite, 'a', sizeof(pbufWrite));
cfile.Write(pbufWrite, 100);
cfile.Flush(); -
Hello. I am trying to write some wave data into pcm file. I pass this data and its length to the write function but it raises this exception
An unknown error occurred while accessing file ..... file path + name ....
I am using following line code to open this file and write contents into itCFile* pFile = new CFile;
pFile->Open("Output.pcm", CFile::modeCreate|modeReadWrite);
pFile->Write((void*)pData, dwLength); // exception raised here
pFile->Close();What could be wrong here? Thanks for any input.
This world is going to explode due to international politics, SOON.
include wchar.h and try
WCHAR* pData = L"Testing Data";
DWORD dwLength = wcslen(pData);
CFile* pFile = new CFile;
pFile->Open(L"Output.pcm, CFile::modeCreate | CFile::modeReadWrite);
pFile->Write((void*)pData, dwLength * sizeof(WCHAR)); // exception raised here
pFile->Close(); -
The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.
This world is going to explode due to international politics, SOON.
-
The file opens successfully. The problem occurs when I try to write something using it. Please see me code again, I have commented it there as well.
This world is going to explode due to international politics, SOON.
Supercoder has given you the most likely answer although I agree with Richard you haven't conclusively shown the file is open. If the file doesn't exist you could have a problem because you probably need to open it with CFile::modeCreate | CFile::modeReadWrite Stick a debug point on the fail case and check. For the write you have a single line => pFile->Write((void*)pData, dwLength); Either 1.) pData is not a pointer to an actual array of data 2.) dwLength has not been set or exceeds the data in pData So all you have to do is work out which of the three things you got wrong and fix it. If you can't fix it we need to see how you defined pData and where you set dwLength probably.
In vino veritas