Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Issue reading/writing file

Author
Message
ronin
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location:
Posted: 17th Jul 2017 03:32 Edited at: 17th Jul 2017 03:36
Hi all,
Well, right now I want to do something very simple, Im opening a file to load some data from it and then plot the pixels on screen for later save them to a image, all sounds quite simple, but I have been trying this for a couple of days with no good results, so I went down to the most basic stuff, open file to read and write using plain C, but the fact is I cant open file to read without having a crash, my current test code is really simple and is run before the main DGDK loop, is it wrong to do it this way?, also I have a spinning cube there, I was trying to see if it reached the main loop or not.



Well it compiles right and there is not even a warning but makes me wonder if using the DGDK makes any conflict with these C functions (I know how silly it sounds). Any help will be greatly appreciated =).

Edit: I have a longer code for the pixel plotting I mentioned, but as it was crashing always I decided to make test code you see here, so I use same funcions thats why Im sure Im making something wrong here, but this works in C compiler (like TurboC I mean).
Edit: The code tag works better than I remember, congrats to those handling it =)
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 17th Jul 2017 13:11 Edited at: 17th Jul 2017 13:12
Quote: "my current test code is really simple and is run before the main DGDK loop, is it wrong to do it this way?"

Yes, you can perform file access wherever you want, but of course it is always better to not to have these at the main loop, but at the start or under an user action.

Instead of fscanf, I perform a char by char reading with fgetc, then store its value with atoi:



This sample is for a reading a file with an integer value per line, like:
Quote: "
122
2134
24
42
..."


And stores it into value_read variable.
To do it some other way, replace the newline comparison by a semi-colon or whatever you need.
A sample of your input file would be handy

Hope that helps
ronin
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location:
Posted: 17th Jul 2017 17:05 Edited at: 17th Jul 2017 17:10
Thanks Morcilla, I think I get what you say, then makes me wonder why it is still crashing here, I mean, well you saw my test code, its really simple stuff, and makes the program crash badly, also I added your code to empty DarkGDK template and the program crashes too, could I have something wrong in my installation? I see not many other explanations to this, I set your code like this to be more exact:


Also I can provide the file input that I need to read and the operations I need to perform if you think it will be better, but its more complex, anyway here its my current code for that solution I mentioned I was trying to accomplish:

(This code is suppossed to access DAT file, get each pixel information, plot it into screen and when done save the screen painted section (image) to BMP firmat directly from screen)


The test file is here:
http://www.mediafire.com/file/mo9g34cepzaj2a1/startbar.dat

The code is a bit messed Im sorry I went a little crazy when it started to crash over and over and later I realized it was just file input that was making it crash, the file Im accesing is this one, its Revenant (medieval game) graphic file, it contains a image but stored in a very special format that goes like this, well its little bit hard to explain, but the file is stored using hexadecimal data, and each time we read 2 bytes it can be converted to binary format and then from this binary string we can get the colors for one pixel, a friend told me an example, its better seen this way:

1) We read first 2 bytes (16 bits) from file. It's two hex values, for example E0 F0.
2) We convert them into binary digits. It's 11100000 and 11110000
3) As a single line it's 1110000011110000. Decoding formula is [G3][G4][G5][B1][B2][B3][B4][B5][A][R1][R2][R3][R4][R5][G1][G2].

So,
red is 11100 (binary) or 28 (decimal) or 230 (color as 0-255)
blue is 00000 (binary) or 0 (decimal) or 0 (color as 0-255)
green is 00111 (binary) or 7 (decimal) or 58 (color as 0-255)
alpha is 1

So, result is (230,0,58) or simply pink

Well of course my main goal is to achieve that file interpretation to convert these DAT files into BMP and viceversa, but I think as soon as file input works it should work the same for file conversion code. Thanks a lot for your help this was driving me crazy.

Edit: Well as I said the bigger code isnt tested so these functions to convert into binary or so could be wrong at some point, the issue arrived with file input so I couldnt test alone these functions yet, just in case you find horrible operations in this functions
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 18th Jul 2017 11:42
Alright, guess you got it working then.
Other than that, I don't think there is anything wrong with DGDK installation
ronin
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location:
Posted: 19th Jul 2017 05:35
Sorry, thats my bad english skills, the problem remains and what I meant is that I built that bigger code without testing the file input/output features (as I coded in ansi C long time ago), so I coded all that and it kept crashing, then started to try to isolate the problem and result is that the thing making the crash is the file input/output functions, so I dont know why these cause this trouble, is there any known issues regarding this or my installation is wrong? your code crashes here too, so Im certain i might have something wrong. Again receive my apologies for bad english making it look like I solved the trouble
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 19th Jul 2017 10:47
I do not have my Windows 10 machine at hand, that could make the difference, but I'll give it a try if I can.

In the mean time, make sure that the file exist, and that 'Everyone' has read permissions on it, and run VS 2008 as Administrator, just in case it is a W10 issue.

Also, please post the error message, and confirm the code line where it happens.

Right now, the only strange thing that I see is:

Quote: "v = strtol(p, 0, 16);
"

since the second parameter should be a char pointer, this could give a crash.

Another question would be, Has this code ever worked?
I do not think you have a bad installation, it is very difficult that VS "C" compiled code doesn't work as expected, it must be something else.


ronin
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location:
Posted: 20th Jul 2017 07:17
Thanks for the advice Morcilla, well I set the VS 2008 to be run as administrator and made sure that the file exist and is readable by everyone, in fact it can create the file if it doesnt exist (opening to write I mean) but it cant open to read, or at least after several tests thats what I find not working.


http://www.mediafire.com/view/31babmbd6tkc7cl/error____.png

Regarding strtol well, you are right, but I have seen somewhere its possible to use it like this if we are not looking forward to use the string there, anyway Im trying to run the most basic file input/output code ever, which is the one I posted before just to see if its there as I think and it keeps crashing as seen on above image, the code again is this one, I think I didnt make any change but just in case:


Yes I know it sounds strange, and this is something that never happened to me before and I have run the Dark GDK in different machines with different OS (but never before in win 10). Any more suggestions will be greatly appreciated =).
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 20th Jul 2017 09:07 Edited at: 20th Jul 2017 09:07
Sorry I do not have that much time at the moment.

If you want to speed up this, please zip the DGDK project, along with the file to read, and attach it to your next post.

This way I will be able to run it straight in my Win 7 installation.

That should tell us if it is a Win 10 issue or not
ronin
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location:
Posted: 22nd Jul 2017 03:26 Edited at: 22nd Jul 2017 03:28
Thanks Morcilla, sorry I couldnt post this before, well to focus on the root of this I think you could try the basic file input project thats quite simple here:

http://www.mediafire.com/file/tdhb6i4zf7l4j3v/file_input_test.rar

However yes, my main goal right now is the image converter (kind of) program, which you can download the project here:

http://www.mediafire.com/file/t8txc00q03qc5k7/Igor+R+Project.rar

So if you could try any of them in your machine that will tell us if its only Win10 issue or what, I know you say you dont have much time these days, dont worry, try this when you can, I really thank you for that because Im lost not knowing whats wrong with these C functions when combined with the DGDK
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 24th Jul 2017 15:30
Alright, two things:

1.- Change the build mode of your project from 'Debug - Win32' to 'Release - Win32'. This can be done at the VS top menu. DGDK comes with libraries in Release mode only.

2.- Change this line of code:



To



And the crash will be gone. fscanf needs pointers to work properly

ronin
21
Years of Service
User Offline
Joined: 18th Feb 2003
Location:
Posted: 26th Jul 2017 02:35
Thanks a lot Morcilla, I only tested basic project but you are totally right, it doesnt crash now, I cant believe my memory is so weak that I forgot how to use fscanf, bigger project will be fixed thanks to your big help, I was lost there, I owe you one, thanks you really =)
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 26th Jul 2017 10:46
Glad to help
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 3rd Sep 2017 08:01
Quote: "    //Terminate string security
    strcat(read_buffer,"\0");"


This does not do what you think it does.

strcat relies on the first string being null terminated so it knows at which point to append the second string. If your first string is not null terminated, then this is a potential buffer overrun. If it is null terminated, then congrats, your string now has two null terminators.
"Jeb Bush is a big fat mistake" -- Donald Trump
https://vt.tumblr.com/tumblr_o2rvwdLLSF1rmjly4.mp4
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 4th Sep 2017 10:01
Uh

I think it terminates the string with a null.

So why is it a potential buffer overrun? That would be the case if the string didn't have that ending "\0"

Remove that "\0" and the whole array will be a single string...

TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Sep 2017 16:14 Edited at: 4th Sep 2017 18:05
@morcilla
No, it does not append '\0' if the destination string is not terminated. That's what you're trying to do with it, right?

Read the documentation:
http://www.cplusplus.com/reference/cstring/strcat/

Quote: "destination
Pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string"


The destination string must be a valid C string (i.e. it must be null terminated), otherwise strcat will overrun the destination buffer until it finds a null (at which point it will copy the source string and write it into invalid memory).

For example, the following program crashes:


And this program will do nothing at all:


All in all: Don't use strcat to terminate a string. The correct way would be:



and of course change your overflow check to make space for the null terminator:


And the even better way would be to use std::string.
"Jeb Bush is a big fat mistake" -- Donald Trump
https://vt.tumblr.com/tumblr_o2rvwdLLSF1rmjly4.mp4
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Sep 2017 19:21
Here's the proper C++ way to do it. It's much safer:

"Jeb Bush is a big fat mistake" -- Donald Trump
https://vt.tumblr.com/tumblr_o2rvwdLLSF1rmjly4.mp4
Morcilla
21
Years of Service
User Offline
Joined: 1st Dec 2002
Location: Spain
Posted: 5th Sep 2017 11:28
Alright I get the point.
Many thanks for the advice, I'll have it into account *thumbs up*

Login to post a reply

Server time is: 2024-11-16 16:42:38
Your offset time is: 2024-11-16 16:42:38