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 / Writing to Files

Author
Message
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 3rd Apr 2011 10:09
Hey folks.

So iv been trying to read and write from a file for a while now. So far iv tried the D-GDK commands. The read commands are ok, but the write commands dont seem to work. After some research i found that its generally not a good idea to use these commands, but instead to just use fstream etc.

However now im having an issue with using the normal fstream commands. In that whenever i include the library i get link errors:

Warning LNK4098
Error LNK2019
Fatal Error LNK1120

i presume that its because somewhere withing the D-GDK engine those libraries are already being called and the calling here conflicts with the prior ones in use. I cant use the fstream commands without including those libraries however.

So does anyone know how i can go about using the commands i need?

Any help is much appriciated.

"Knowledge is Power"
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 3rd Apr 2011 18:09
Have you set the runtime library to Multi-threaded(/MT)?

Project->properties->configuration properties->C/C++->code generation->Runtime library

Its probably better to post the full linker error as people may not know them just by number. if you think its too much to post then put them in 'code' tags to keep it tidy.

Hope that helps.

Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 3rd Apr 2011 20:32
hey matt. I had not set the runtime library to MT (it was instead MTd) however, having set it to MT does not seem to have fixed the issue. I shall post the LNK errors below for a bit more clarity.



Some other info, im running Windows XP pro 32bit. AMD64 2.8gz Dual Core (FX62 version, currently not overclocked). 2gb ram, radeon 4800x2 graphics card.

Thanks alot for your sugestion dude, do you have any other idea what could be causing this?

"Knowledge is Power"
DeadTomGC
14
Years of Service
User Offline
Joined: 11th Aug 2010
Location: LU
Posted: 3rd Apr 2011 21:10
Also, you should release instead of debuging.

Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 4th Apr 2011 02:57
@Dead Tom

I ran it also in release mode, and instead got a accsess violation error:



"Knowledge is Power"
Matty H
15
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 4th Apr 2011 15:11


Thats what I get with the wrong runtime library, pretty similar. I would double check my advice further up, perhaps try setting the configuration in the top left corner to 'all configurations'.

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 4th Apr 2011 20:01
I use fopen() with fread()/fwrite() for all mine. It works beautifuly especially since you can do an entire class with it.

The fastest code is the code never written.
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 5th Apr 2011 02:15
@matty yes, your errors seem to be identical, which is rather upsetting as i have made no progress with fixing the issue.

I double checked the runtime library and i just want to make sure there are no other options that i have gone and messed up. So here is what i have it set as:



I should also make an addendum to my previous post. I dont instantly get an access violation error first the compiler throws this at me:


then after i click yes to that, i see the accsess violation error:


does this make any sense to anyone?

"Knowledge is Power"
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 5th Apr 2011 10:31
Here's a screenie for you that may help.

JTK

Attachments

Login to view attachments
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 5th Apr 2011 13:43
sadly those ignore library settings are exactly what i have set too.

"Knowledge is Power"
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 5th Apr 2011 21:13
The access violation doesn't have anything to do with your linker problem, since access violation is a runtime error. If you can compile, only it doesn't run, then actually the linker issue is solved. Why it crashes is a different problem.

On the screenshot I see a terrain example code. Maybe the access violation happens because it cannot load some of the bitmaps that the terrain should be built from, and then it crashes when it tries to update the terrain for the first time.

For the linker problem I can only say the same things that have already been suggested: /Mt and ignore libraries, but it seems you already have those settings.
Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 5th Apr 2011 21:40 Edited at: 5th Apr 2011 21:42
EDIT: lol i didnt read correctly, have you ignored those libs? maybe you are going off bounds with an array?

hi
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 10th Apr 2011 22:40
Hey folks. So i managed to get the program working in release mode. It was kinda weird, all it took was to create a new project and put all the code files into it o.O

but anyway, it all works fine in release mode but is there a way that i can get it working in debug mode? I know dead tom said that it has to be in release mode, but surely there is a way to run the program in debug with the library inclusions.

any help is appriciated, thx.

"Knowledge is Power"
Cetobasilius
14
Years of Service
User Offline
Joined: 29th Dec 2009
Location: Mexico
Posted: 11th Apr 2011 00:01
yes, you must exclude some libraries like libcmt.lib and atls.lib? dont remember the exact name but the compiler will tell you which to exclude.

hi
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 11th Apr 2011 12:48
i believe i have all the appropriate libraries ignored: msvcrt, libcmtd, atls. However i also noticed in the errors i was getting:


that it mentioned libcpmtd and libcmt. So i set the compiler to ignore libcmt and got in excess of 1000 errors, ill go out on a lim and say its a good idea to keep that library. So i tested the other library mentioned and set that to ignore and got the following errors instead:



and i cant make head or tails of those errors. o.O

"Knowledge is Power"
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 11th Apr 2011 19:39
Ignore libcmtd et Al. As shown above.

In debug build, set the preproccessor to undef _DEBUG.

That should do it.

JTK
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 12th Apr 2011 00:05
hmm, you may have to explain that one a little more. Do you mean just putting "_DEBUG" in the preprosessor Definitions like this:



if so, it has not availed me anything. I still get the same errors. Or am i doing it wrong?

"Knowledge is Power"
JTK
14
Years of Service
User Offline
Joined: 10th Feb 2010
Location:
Posted: 12th Apr 2011 02:03 Edited at: 12th Apr 2011 03:29
No. Actually you don't want it in there so take it back out. It seems you're missing a couple others too. _WIN32 for one but the other escapes my memory. When I get home, I'll screenshot all of my settings and you can see what's different.

It may be easier for you to create a new GDK project from the wizard and copy your files over. Something's missing...


JTK

Edit: I must apologize! I don't have anything in the preprocessor settings for Debug build. Perhaps your include/lib directories are out of sync? What version of GDK you have installed?
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 12th Apr 2011 23:47 Edited at: 12th Apr 2011 23:49
When you get those error messages with malloc_dbg and free_dbg, the usual solution is to set the runtime libraries to Mt instead of Mtd. (It's already set that way in the default release configuration, so it works in release mode.)
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 24th Apr 2011 17:58 Edited at: 24th Apr 2011 20:38
@mireben Thanks alot dude, that seemed to sort it, my breakpoints are even working now

However every problem that is solved only seems to breed yet more problems. And, as ever, i cant seem to make sense of it.

Now the problem is that i just cant write to files. I can read them perfectly well, but writing has completely ceased to function. I am pretty sure that this is not a code problem, but i shall put my code down below. The file i am attempting to write to is in the main directory for the game, so im pretty sure its nothing to do with that. I have tried changing the file type but no avail either. I have run it in both release and debug mode and tried F11'ing through the code where something curious happens. As the code reaches the Scores.open("Scores2.csv") line, it goes to the disassembly, opens a window and asks for the location of a variety of source files that i havnt heard of (never asking for the same one twice) such as fiopen.cpp, mbstowcs.c, setlocal.h, tidtable.c etc etc.

I dont know weather this is the source of the problem or not, but what is annoying is that it compiles and runs without any error, these windows looking for source files only occur when i step through the program using f11.

This could be something really simple, but im really confused, it seemed to be able to write to files before but since i managed to fix some previous errors it now seems incapable of doing so and dosnt even show a warning or error message.

EDIT:: on a side note, does anyone know how long this post approval thing will last for? Having to wait 4-6 hrs for my post to appear is rather annoying.

Any help is much appreciated. Thanks!

"Knowledge is Power"
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 25th Apr 2011 14:12
I can only tell you that the windows you see after F11 are normal. The debugger tries to find the source code for the file stream open function, which of course it doesn't find, so it asks you to search for the file. I've never tried to find source code for built-in functions but I very much doubt that it's included in Visual Studio, so you won't be able to debug into that function. You can use F10 instead of F11 to debug without going into function calls.

Since the open function doesn't return any error code, there is not much to go on here. You didn't post the code. After opening the file, do you have an is_open check before trying to write to the file? That will at least tell you if the file is not created at all, or maybe it's created but in a different directory than where you look for it? Try to give a simple absolute path together with the file name and see if it works that way.
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 28th Apr 2011 15:50
thanks for the suggestions dude, but sadly it has not solved the problem. Here is the code (apologies for not posting it last time).



I put in the message boxes for debugging. The first box dosnt appear which is correct because there should be no file open. The 2nd does appear which is fine because i just opened the file and finaly the third appears (multiple times obv) which shows that its entering the loop.

I also added the full file path when opening the file, to make sure it wasnt trying to look somewhere else.

Is there something completely obvious that i am missing? The file already exists but i have tried deleting it and making the program the create a new one just before it goes to open it. I have tried all the combinations of ios::in, out, app, ate, trunc etc.

Just not understanding this one

"Knowledge is Power"
Mireben
16
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 30th Apr 2011 11:25
Is Scores declared as ofstream? If yes, you don't need the ios::in | ios::out part. Also the path should not contain a link (Desktop\\My Documents must be a shortcut on the desktop), but you tried without path as well so that's not the main problem.

It's a mystery because when I tested this, when the file was not created, then the message boxes did not appear either. When the message boxes appeared, then the file was created and written correctly. I have no idea how it is possible that you get the message boxes but no file... sorry that I can't help more.
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 10th May 2011 19:04
Quote: "Is Scores declared as ofstream? If yes, you don't need the ios::in | ios::out part."


Its declared as



because ofstream didn't work with my getline function. Does this matter?

"Knowledge is Power"
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 11th May 2011 19:17 Edited at: 11th May 2011 19:19
Ok so there have been some odd developments. I changed the fstream for ofstream and removed all my getline functions and tested the program. and it seems to write to files fine, there was me thinking that fstream was suposed to be able to read and write. So i tested that by changing the stream types again still keeping the getline functions out and the program works fine once again.

So does anyone know why my getline would disrupt the program so much. I shall put the function below that establishes the score table (the function with getline in it).



EDIT:: the purpose of the function is to get the data from the score file and store it in an internal array that is later used to print the scores to the screen and to compare to the player score so you know when a score enters the high scores.

"Knowledge is Power"
Lord_Migit
13
Years of Service
User Offline
Joined: 3rd Apr 2011
Location: Dundee, Scotland
Posted: 13th May 2011 02:22
Ok from what i understand, i beleive i have solved the problem, im posting in case anyone else has this problem in the future.

From what i understand i think after reading the scores initially in the InitializeScores function where i save them to the internal array the "getting" pointer, for lack of better terminology, was placed at the end of "Scores". So even when i opened a new file if i used the same fstream ("Scores" in this case) then the pointer to read the information was still at the end of the file.

So the way i solved this is by putting these lines:


just before "Scores.close();" in the initialize function.

"Knowledge is Power"

Login to post a reply

Server time is: 2024-10-02 17:33:07
Your offset time is: 2024-10-02 17:33:07