![]() |
| ||
| file read and buffer problems So finally after a little waking up i fixed my write to a file in raw binary problem. Now I am trying to load the same data from the file i created. Here is the code I came up with. #include <fstream> The struct OURCUSTOMVERTEX is defined in the vertexs.h header file and contains 3 floats (x, y, z) and a unsigned long (color). When I build it no errors are returned at all, not even so much as a warning. But when I run this a system error as follows occurs: Unhandled exception at 0x7c9377a2 in saveverts.exe: 0xC0000005: Access violation writing location 0x00030ffc. Through a little breaking i found that the problem is coming in at the following portion of code: for (int temp2 = 0; temp2 < i_CountInds; temp2++) After stepping through the function, I found that the problem is originating here: f_DataFile.get(buffer, sizeof(long)); At this point with my sample run the buffer should read 09 00 00 00 ... as the long stored in the file by the save routine stores an unsigned long with value 9, which is what is being read here. Instead the buffer reads 00 09 00 00, which is 900 hex, which is 2304 decimal. here inlays the problem, i believe. In any event, from my perspective this is the root of the problem, and unfortunately I am unable to troubleshoot it further on my own and must ask for help. Following is the SaveVerts routine, in hopes that it will help: int SaveVerts(char *s_fName, OURCUSTOMVERTEX *p_Verts, short *p_Indices, long i_CountVerts, long i_CountInds) And finally this is the section of code in assembly (for all the ASM gurus out there) once the program crash and debug pops: 7C93779C mov edi,edi Thanks in advance for any insights. |
| ||
| Re: file read and buffer problems The assembly line only points to where the IP was when the error was identified. (So it doesn't help.) In any case, the problem is that unsigned char buffer[ n ]; creates a variable (buffer) which is, by itself, a pointer to a list of bytes. The number of bytes in the list is known to the compiler, but otherwise the array definition is just like saying:1. reserve n bytes on the stack 2. create a variable unsigned char *buffer;3. let buffer point to the first byte just reserved on the stack Hence, the problem occurs when you say: memcpy_s(&i_CountInds, sizeof(long), &buffer, sizeof(long));The types of the desired parameters are: pointer to bytes, int, pointer to bytes, int. The types of the arguments you give it are: pointer to bytes, int, pointer to pointer, int. So, the first time you read something you are changing the address in 'buffer' to point to some other, random position in memory. The next time you try to access the buffer you get an access violation --meaning that you tried to access memory that doesn't belong to you. Remember, buffer by itself is a pointer. You can dereference it with*bufferor buffer[ 0 ]but without the * or [] it is just another pointer variable. So, to fix your code, use memcpy_s(&i_CountInds, sizeof(long), buffer, sizeof(long));Hope this helps. |
| ||
| Re: file read and buffer problems While your explaination made perfect sense, it did not solve the problem. I made what should have been the required change and still get: Unhandled exception at 0x7c9377d5 in saveverts.exe: 0xC0000005: Access violation writing location 0x00030ffc. You are very sharp to catch this error... and I am slow for not. Though this was a problem... it is not THE problem. Further assistance would be appriciated. |
| ||
| Re: file read and buffer problems You changed every instance of &buffer to buffer in both of the routines posted above?If you have, I don't know where the problem is. My quick glance says they look alright to me. Unless you are reading or writing too many bytes for your buffer or integer types somewhere... Does your custom vertex structure have any pointers in it? |
| ||
| Re: file read and buffer problems Yes i changed them all in both functions. As far as writing and reading too many bytes, i am writing and reading the exact same amount because i structured the LoadVerts routine from the SaveVerts routine with an exact copy and slight modification. Additionally after each of my runs, i am checking the chris. raw file against the output i should have based on my knowledge of how raw data is stored in files. They come out identical. My struct is as follows and contains no pointers: struct OURCUSTOMVERTEX Can you see why this is frustrating me so? |
| ||
| Re: file read and buffer problems Maybe you are just paying the price for using ifstream.get()? Is there a byte with the value 10 in your data file? (You should be using ifstream.read().) Let me know if that fixes it... (ATM I don't think it will...) |
| ||
| Re: file read and buffer problems I am an IDIOT! Okay just to make myself look retarded.... i will explain what happened. Review the original code for LoadVerts. Look near the bottom at this line: memcpy(&p_Indices[temp2], buffer, sizeof(short)); The problem here is that the array that needs to be written to is p_temp_Indices (declared in this function) and then copied to the original array. Just goes to show that something small can cause a cog and stump a few..... WOW! I should be more careful..... LOL! |
| ||
| Re: file read and buffer problems Heh heh heh... I do stuff like that all the time... :twisted: Please fix those get()s to read()s though... |
| All times are GMT -4. The time now is 4:09 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC