Archived

This topic is now archived and is closed to further replies.

Fwrite problem. Causes Access Violation

This topic is 5584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

BOOL CEditSaveFile::WriteEdit(long offset, char *data, unsigned int bytecount)
{
	int error = 0;
	int bytes = bytecount;


	if((error = fseek(savefile, offset, SEEK_SET)) !=0)
		return FALSE;

	
	if((error = fwrite(data, sizeof(char), bytecount, savefile)) != bytes)
		return FALSE;
	else
		return TRUE;
}

 
How my function is coded
	if(!edit.WriteEdit(0x000CECA0, (char *)0x90, 1))
		printf("error in WriteEdit");
 
How I call it. This causes an access violation on the fwrite line. Anyone know what causes it and how I can fix it? Ive been trying all day . Thank you, Maega

Share this post


Link to post
Share on other sites
This is too much out of context to spot any errors, or at least for me. I guess bytecount and savefile are vars set somewhere else, and they are the only ones I can''t verify are set right. You sure that the savefile file handle is valid?

Share this post


Link to post
Share on other sites
Eh...
quote:
edit.WriteEdit(0x000CECA0, (char *)0x90, 1))

What have we here? You send a pointer to address 0x90??? I don''t think that address is very popular. What are you trying to write to your file?

Share this post


Link to post
Share on other sites
Ok. I think you want to write ASCII 0x90 to your file, right?

What you do there is to have the value 0x90, and tell the compiler that that value actually is a pointer to a char. Not that it shall create a pointer to a char of that value. I'm very bad at C-stuff so the following could surely be better made, but you need to do something like this:

char szTmp[1];
szTmp[0] = (char)0x90;
edit.WriteEdit(0x000CECA0, (char *)szTmp, 1);


EDIT: You could of course have something like: ..., (char *)"X", ..., but I couldn't remember how you could squeze in an ASCII code like that.

[edited by - CWizard on August 31, 2002 4:57:41 PM]

Share this post


Link to post
Share on other sites
It's for a saved game editor class actually .

90 just popped in my head because I use it alot when noping DMA stuff.

savefile is a FILE *. It is correct because I call a edit.OpenFile("test.sav"); before.

bytecount is a parameter

[edited by - Maega on August 31, 2002 5:02:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Maega
bytecount is a parameter
Yeah, I missed that

Have you nailed the problem yet? A very probable cause, is that you tell fwrite() to read from address 0x00000090.

Share this post


Link to post
Share on other sites
If you mean an array of chars, that is, with more than one element? That is easy, just make a normal string. As stated you could do something like this:

edit.WriteEdit(0x000CECA0, (char *)"Stuff to write to log", 21);

Or, you could do it the hard way:

char szTmp[] = "Stuf to write to log";
edit.WriteEdit(0x000CEA0, szTmp, sizeof(szTmp));

There you have szTmp as an array. Or did you mean something else?

EDIT:

Note that in the latter case you would also get the null-byte terminator written. You could avoid this by having the bytecount = sizeof(szTmp) - 1;

Then you could of course allocate an array of chars, which you latter fill in with (binary) data:

#define BUF_SIZE 1024
char szBuffer[BUF_SIZE];

for(int j = 0; j < BUF_SIZE; j++)
szBuffer[j] = j;
edit.WriteEdit(0x000CEA0, szTmp, BUF_SIZE);


[edited by - CWizard on August 31, 2002 5:20:44 PM]

Share this post


Link to post
Share on other sites
Ok.. I got it..

I tested it with this

char szTmp[] =
{
0x90, 0x92, 0x34, 0x35
};


if(!edit.FileOpen("test.sav"))
printf("error");

if(!edit.WriteEdit(0x000CECA0, (char *)szTmp, sizeof(szTmp)))
printf("error in WriteEdit");


It works, I just get warnings about truncations. They don''t affect anything though .

Share this post


Link to post
Share on other sites