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.

Newcomers DBPro Corner / Difficulty writing to a file...

Author
Message
Game pro
20
Years of Service
User Offline
Joined: 21st Mar 2004
Location: Usa
Posted: 1st Oct 2008 04:02 Edited at: 1st Oct 2008 04:20
Okay, so to start off. I've successfully created and stored information to a file in DBPRO before. However, using the write string command doesn't seem to be working correctly for some reason. I'm using the latest beta build of Darkbasic Pro 7.




This function loads a save box, then asks for a file name. It's then suppose to create a file using that file and path name. Then write a string into that file. The string fails to write though, and when I open the file it contains 0 bytes of data.

[edit]
Nvm, if I just learned to use the search tool more I would have found the answer by now. Apparently you can't make the file first, because you can't write a string to a file that already exists.

To make this thread less useless, how would I go about editing a file that already exists(seeing as you can't write to a file that exists already)?

Check out my developer forums at:
http://archaicstudios.net/forums
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 1st Oct 2008 04:43 Edited at: 1st Oct 2008 04:50
Quote: "To make this thread less useless, how would I go about editing a file that already exists(seeing as you can't write to a file that exists already)?"


I would do it like this:

1. Open the file for reading

2. Store the contents of the file in an array

3. Close the file and delete it

4. Open the file for writing

5. Begin to write the array's contents to the file, stopping when you get to the line you want to add something to. For example if you wanted to put "HELLO" on the 100th line of a 215 line file, then you would write everything up to line 99 and then continue to follow the steps below. If you wanted to add "HELLO" to the last line, then you would write all 215 lines and then follow the steps below. Step 7 would not be used in this case.

6. Write your string

7. Finish writing the array's contents to the file if you haven't already.

8. Close the file.


Of course you may already need to know how the data in that file is written in order to do it this way... Maybe there's a better way to pre-store the data? This should be OK if you know the file's contents already, however. (ex. if you wrote 10 strings, then 10 floats and you knew the order they were written beforehand.)

Login to post a reply

Server time is: 2024-11-25 16:37:37
Your offset time is: 2024-11-25 16:37:37