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.

DarkBASIC Discussion / Writing some log files

Author
Message
Quirkyjim
17
Years of Service
User Offline
Joined: 18th Oct 2008
Location: At my computer
Posted: 11th Nov 2008 22:37
Okay, I've written a log function for one of my programs, but it's not working. I'm not exacty sure what's going on, so help would be greatly appreciated.

The code is

Thanks in advance!

~QJ
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Nov 2008 03:54
Hello,

Open to write will fail if the file exists. If you try and write two or more log files on the same date, you'll get an error (because you are naming your file the date).

Try :


That will replace the current date file with the new one. If you want to keep a running log and not delete the file, since function variables in DBC are static (that means their value stays around in the function until you change it inside the function), you could have a count that increases and is tagged onto the date$ every time the log function is called:



Enjoy your day.
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 12th Nov 2008 20:19
Also, you probably shouldn't close the file at the end, since open to write only works for files that don't exist. To make sure that it creates the file you can use:



Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Quirkyjim
17
Years of Service
User Offline
Joined: 18th Oct 2008
Location: At my computer
Posted: 12th Nov 2008 21:58
Is there any to add on to an existing file?

~QJ
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 13th Nov 2008 04:51
There's no APPEND function per se. BN2 has a very good suggestion that is leaving the file open until you are done writing the file for the whole session. Then close after everything is finished.
I think BN2 meant to test if the file didn't exist before writing to it:
if file exist(date$+"log")=0 then open to write 2, date$+" log"

Or, you can manage the appending yourself. It seems that you are storing strings in your file. That makes things nice and easy. When you write a string to a file, each line ends with a carriage return and a line feed. This means there is a way to count the number of lines in the file. This means that the file can be looked at as an array. An array is a list of values. Since each line in our file ends with a certain number (13 for carriage return and 10 for a line feed), by counting the carriage returns (or the line feeds) we can get a count of how many lines are in the file. That gives us a size for an array to dimension.

Why am I talking about arrays? Well, we can make the storage and retrieval of the log file very easy by saving and loading the file as an array. The beauty of this is, once we get the count of the lines, we can DIMension our array a little bigger so we can add another log entry to it. Then we just save the whole array back to the log file with the new entry.

How do we get the count of the lines? We OPEN TO READ the file, then read each byte. We'll just count carriage returns so we check if the byte value is equal to 13. If it is, we increase our count.

Once we have read the entire file and have the count, we dimension the array to this size:
DIM log$(count)

We load the file into the array:
load array date$,log$(count)

We add the new entry
log$(count)=get time$()+action$

Then save the array
save array date$,log$(count)

A few things to note:
All file operations run much faster if you have used SYNC ON at the beginning of your program. It took about .394 sec to read a 6500 line file in some tests I ran with SYNC ON called some time ahead and it took 5 secs without it.

get date$() returns a string with slashes "/" in it. Don't save the file with that name because slashes are file operation characters and can cause problems under certain conditions if in the filename. You can remove or replace the slashes using a for next loop, and mid$().

Arrays start counting at 0, that's why we can redimension the array with just the count that is returned from the open to read part. By DIMensioning the log$() to count, we actually have one more element than the size of the file.

The first time creating a log for a particular date, the file will not exist. You'll have to test for this condition and create your log array as
count=0
DIM log$(count)

Then update it and save it just like the above.

I wrote up a complete function that does all of this just to test out the idea and it works great. See if you can do the same.

Enjoy your day.
Quirkyjim
17
Years of Service
User Offline
Joined: 18th Oct 2008
Location: At my computer
Posted: 13th Nov 2008 23:13
Two things: one, is there any way to get the date without the dashes? Some ingenious finction that takes them out? That might be why the fles have not been created!

second: you wouldn't happen to be able to give me that function would you?

~QJ
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 14th Nov 2008 01:15
The date thing is a matter of looking at each character in the string and removing or replacing it with something else:



And for the second point, if you mean the entire log file function, I think you should try and work it out yourself. Try and piece together the instructions I laid out in the previous post.

The counting might be the only unclear part:


Enjoy your day.
Irojo
18
Years of Service
User Offline
Joined: 21st May 2008
Location: Eating toast.
Posted: 14th Nov 2008 16:28 Edited at: 14th Nov 2008 16:29
@Quirkyjim
Quote: " is there any way to get the date without the dashes? Some ingenious finction that takes them out?"


I believe this is what you are asking for. If so, it is fairly simple. The code has Rems that explain anything that you are new with. Enjoy!
The function:

In use:



I download memory.
Quirkyjim
17
Years of Service
User Offline
Joined: 18th Oct 2008
Location: At my computer
Posted: 15th Nov 2008 00:27
I'm just about jumping out of my seat now! It worked! Thank you guys so much!

Fyi, this is for a school project, so I'll put you guys in the credits. Thank you!

~QJ
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 21st Dec 2008 16:52
ok i had wondered about this also so a question about latches if you set Temp$="_" then set date$=Temp$ that Changes Date$ from the date to a Underscore doesn't it?

And Irojos does the

storage$=Storage$+Mid$(Your$,Remove)

Does that replace the character? thats what it looks like jw
Latch
19
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Dec 2008 22:41 Edited at: 21st Dec 2008 22:43
@Caleb1994
Quote: "a question about latches if you set Temp$="_" then set date$=Temp$ that Changes Date$ from the date to a Underscore doesn't it?"


The line actually reads temp$=temp$+"_"
The string is rebuilt character by character and if the character is a / then it is replaced with _ .
Try running the example and you'll see the result printed on the screen.



Enjoy your day.
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 22nd Dec 2008 00:59 Edited at: 22nd Dec 2008 03:51
oh i see but just for anyone else rearanging the code helped me to see it so heres my slightly changed code



that just helped me thanks latch for this advice i had wondered how to write to an existing file. i knew there had to be a way

[edit]
Thanks so much guys with this info i now have saved scores in my Pong Game lol
[/Edit]

Login to post a reply

Server time is: 2026-07-05 16:28:21
Your offset time is: 2026-07-05 16:28:21