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 / Question regarding reading and writing to files with darkGDK functions

Author
Message
Gaslov
12
Years of Service
User Offline
Joined: 12th Feb 2012
Location:
Posted: 12th Feb 2012 22:04
Hi guys,

I am trying to read and write to a text file using darkGDK functions and goes as follows:



What I am expecting this code to do is take keyboard input from user and store it in variable 'Text'. I then want it to write to the file Game1.txt what is stored in 'Text'. Then I want to write AAAA to the file. After this, I want to read the first line of file Game1.txt and store it in 'Text2'. Then I want to write a third line that contains what was stored in 'Text2'.

However, when I open the file, Game1.txt, I get the following:

abc <--- user input
AAAA <--- program input
MZ <---- what the?

I cannot understand why I am getting this 'MZ' stuff at the end and not the user input as expected.
xix psycho xix
15
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 12th Feb 2012 22:50 Edited at: 12th Feb 2012 23:06
Is that code a direct copy? I ran it and it worked exactly as expected. If this was just an example fragment it would help to see the rest of the code so that we know more of what is trying to be accomplished.

Edit: I just realized something. You are trying to read from a file that has been opened for write. I didn't read the OP close enough. You are not getting the line to print into the file a second time because you are trying to read it from a write file. Close the file, reopen it for reading, copy the contents to a second temporary file, and then copy it. This snippet is more what you are looking for. It should give you an idea of what needs to happen to do what you are looking for:

Gaslov
12
Years of Service
User Offline
Joined: 12th Feb 2012
Location:
Posted: 12th Feb 2012 23:19
Thanks a ton! Do you know why it creates a gap between the first two written lines and the third line? If I enter 'abc' I get

abc
AAAA

abc

instead of:

abc
AAAA
abc
xix psycho xix
15
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 13th Feb 2012 01:12
Haha yes I noticed that when I tested my own code. Give me a bit to re-work it and I will make an improved example. Just a question, how much c++ do you know? working with darkgdk files is similar to working with c++ files, same copying procedure and everything. It's just slightly easier to work with the opening them part in DGDK. It would help to know tho why you have to copy that line to the end of the file like that. I may be able to help you better with a little more info, but I will look over it again and see what I can come up with
Hassan
14
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 13th Feb 2012 07:50
avoid using DarkGDK's file functions and dbStr and pretty much anything with char* in it, i believe they all cause memory leaks, the std library or C methods work just fine and they're pretty easy, you are just reading/writing lines, plus they have a lot of other features

Gaslov
12
Years of Service
User Offline
Joined: 12th Feb 2012
Location:
Posted: 13th Feb 2012 23:37
I am a novice programmer and am programming for fun. I am trying to create a hexagon turn based board game (but on the computer obviously) and what I need to do is create a file that contains the hexs that will store everything that occupies that hex. So my game has many different resources and units so my hex file, if it had only three tiles would read something like:

HX01000200000000...so on and so forth
HX02030000000100
HX03000000020000

Where the 0's represent something that is contained in that square. I need to be able to read and write from this file a lot as everytime you move a unit or resource, it needs to take from one hex and give it to another. I would then email this file to a friend for his program to read that would update his game with the new information. The code I posted was just a test to make sure I understood how it all worked so I wanted to test taking input, writing it, reading it, and then writing again what was read.
xix psycho xix
15
Years of Service
User Offline
Joined: 15th Sep 2008
Location:
Posted: 14th Feb 2012 01:50
You are better off storing this kind of information in a class instead of a file and then writing it to the file when the program exits. You could also do an occasional file write just to make sure you don't lose the data, but if you store it in a class then you could just erase the existing file and dump the whole hex structure to the file at once. I'm not exactly sure what kind of board game you are trying to draw. I'm sorry I guess I just don't understand your concept Does each hex go in a separate file? Is there a master file for all hexes? Here would be the basic idea:



Now each time that a hex changes you change the class. Every time you want to store your hex data, call the write function. This will make it alot easier and more efficient as disk writing takes much longer than accessing class members in memory. Especially in a game you should do everything you can to avoid writing to a disk! At least that is my opinion. If you need more information about writing new classes you should look up a tutorial or even better buy a C++ book if you don't have one. Having a reference manual on hand is a good idea in any case, but learning about classes is almost a must when programming in C++. Plus, let's face it, object-oriented programming is good practice anyways!
WLGfx
16
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 14th Feb 2012 01:54
Instead of trying to use GDK's files, try looking into the stdio.h file handling, it so much easier. This is probably an easier example using a binary file as well as many other file handling stuff -> C Tutorial – Binary File I/O

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 15th Feb 2012 01:16
WLGfx is exactly correct IMO. The DGDK file system is not very good and more importantly (for me) stdio.h file handling can do structs and classes as data types to load/save natively.

Saving/loading memblocks is about the only thing I use the DGDK file system for.

The fastest code is the code never written.

Login to post a reply

Server time is: 2024-03-19 10:54:37
Your offset time is: 2024-03-19 10:54:37