Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Clueless on error

mad_hattermad_hatter Member Posts: 25
Hi all, forgive me if this is a newbie problem but I can't figure it out. What I'm doing is opening a file, then trying to read it in. I get an error when I try to do the read.

"An unknown error occurred while accessing an unnamed file."

I've traced the problem to this function

ASSERT(AfxIsValidAddress(void*, uint));

From what I've read about this is it checks to make sure there's enough memory? Here's my code anyway, you'll see what I'm doing. Maybe some of you gurus have seen this before. Thanks.

[code]
CFile dibFile;
//open file
if(!dibFile.Open(_T("BITMAP1.bmp"), CFile::modeRead))
AfxMessageBox("Didn't open file");
//create memory for teh file header
BITMAPFILEHEADER bmpFileHead;
//read the header into memory
if(!dibFile.Read((void*)&bmpFileHead, sizeof(BITMAPFILEHEADER)))
AfxMessageBox("Couldn't read the file");
//check to make sure the file is a valid bitmap
if(bmpFileHead.bfType == 0x4d42)
{
AfxMessageBox("Is a bmp");


//need the length of the file for further processing

DWORD fileLength = dibFile.GetLength();


//take the size of the header away from the length, we already have the header in memory

DWORD size = fileLength - sizeof(BITMAPFILEHEADER);


//create a pointer to the bitmap data as a chunk of memory

BYTE* pDib = (BYTE*)::GlobalAlloc(GMEM_MOVEABLE, size);
int x = ::GetLastError();
SIZE_T y = GlobalSize(pDib);
[/code]
get last error returns 0, so the memory is being allocated, and GlobalSize is returning the same number as size. that means the memory is definately allocated right??
[code]
//read the data into the array
[green]//this next line is causing the error
//I followed the program into the CFile::Read function
//where AfxIsValidAddress is checked. [/green]
[red]
dibFile.Read((void*)pDib, size); [/red]
dibFile.Close();
...
[/code]

Comments

  • mad_hattermad_hatter Member Posts: 25
    Ok, I've figured out my problem to an extent. I was reading past the end of the file. Instead of using size, I tried 100. But this doesnt' make sense to me. I thought that once you read from a file, there's a pointer that moves to the last byte read. Then if you read again, it starts from there? Obviously I missread that, because if that was true my code should work, shouldnt' it?
  • mad_hattermad_hatter Member Posts: 25
    found it. thanks anyway. hehe guess I should have tried a little harder before posting.

    Anyway, turns out the memory was bad for reading. Not really sure why though. But I changed from GlobalAlloc to malloc and it works perfectly. Still have some issues when rewriting to the new file though.
  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    ______ { http://forcoder.org } free ebooks and video tutorials about ( PL/SQL Swift Java PHP C++ Delphi Assembly Objective-C Python C# C Ruby Go Scratch R MATLAB JavaScript Visual Basic .NET Visual Basic Perl F# Julia LabVIEW Lua Transact-SQL Scheme Apex Crystal Prolog Rust VBScript Dart ABAP SAS Ada Logo D Hack Awk Erlang ML Alice Kotlin Clojure COBOL Bash Lisp Scala Fortran FoxPro ) _____

Sign In or Register to comment.