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 / How to move a word from one text file to another?

Author
Message
Mark Garrett
Reviewed AGK on Steam
19
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 3rd Nov 2010 05:47 Edited at: 3rd Nov 2010 06:06
if file exist ("file_2.txt") then delete file "file_2.txt"

Open to read 1,"file_1.txt"

Open to write 2,"file_2.txt"

Read String 1,T$

Write String 2,T$

___________________________________________________________________

The above works fine. Try it you'll like it.

It's more fun to Code.
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 3rd Nov 2010 10:52
I'm pretty sure that won't work, as you haven't included a "close file" anywhere.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 3rd Nov 2010 17:19 Edited at: 3rd Nov 2010 17:20
@ =PRoF=:

It'll work it's just not proper coding to leave files open like that. I don't like leaving files open like that either.

@ Bubba:

If you're going to teach newbies how to do something do it right.

Any file you open you should always close when you're done with it. Especially if there are other files being opened later in the code. If newbies are not taught to close all files they open after their done with them they may accidentally try to open an already open file later and may not understand why they're getting an error.

And technically that's not moving a single word but an entire string whether it's one word or 1000 words.

=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 4th Nov 2010 10:56
@Grog:
I just had to test this, and your right (not that I doubted you for one second, lol); I was always under the impression that the file didn't get written until you closed it. I must be thinking of another language.

TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 4th Nov 2010 13:59
Not sure what your problem is, maybe try defining your variable as a string?



If you only want to copy a word, you can get the word with this:



All untested, if anyone can correct me I'd much appreciate

TheComet

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 5th Nov 2010 01:57 Edited at: 5th Nov 2010 01:58
@ =PRoF=:

Thanks for the vote of confidence.


@ TheComet:

He didn't really have a problem he was just showing newbies. Your second code snip doesn't quite work but here's a fix:



The only thing that shouldn't be done was using the variable name of string. It's best not to use variables that are exactly the same as keywords... it may lead to weird errors.

That's a nice way of doing it. I would of used one FOR/NEXT loop like this:



But the less code the better. IanM will always beats us.



TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 5th Nov 2010 11:40
Quote: "He didn't really have a problem he was just showing newbies."


Hmm, I didn't really want to direct what I said to grog, more to Bubba

@ grog

Damn that IanM... We must form a resistance and fight! j/k

Nice snippet, I think without the comments it's shorter


TheComet

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 5th Nov 2010 17:16 Edited at: 5th Nov 2010 17:17
Quote: "Hmm, I didn't really want to direct what I said to grog, more to Bubba"


I saw the word "untested" and the phrase "if anyone can correct me".

Quote: "Damn that IanM... We must form a resistance and fight! j/k"


If there is resistance I'll be on IanMs side.

Quote: "Nice snippet, I think without the comments it's shorter "


Thanks but your code is shorter by one line without comments so you win (not vs IanM of course).

DeadTomGC
14
Years of Service
User Offline
Joined: 11th Aug 2010
Location: LU
Posted: 20th Nov 2010 00:38
Grog Grueslayer, what is the deal with your "Anti-goto order"? I program on a system that doesn't have loops and only has a goto command. It allows for freer, faster and shorter coding. Also, if you use bookmarkers, it is actually easier to understand code that uses goto statements because instead of just seeing an end bracket, you tell what is the end of a loop and where it will jump.

[url="http://www.armchairempire.com/"][/url]
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 22nd Nov 2010 16:54
Quote: "Grog Grueslayer, what is the deal with your "Anti-goto order"?"


The GOTO command creates what we call spaghetti code (click on my signature to see the wiki entry). It makes debugging a lot harder because the code is jumping all over the place. Programs without GOTO have a natural flow that's easier to follow/debug with commands like GOSUB that RETURNS back to the line after the GOSUB and functions that also return to the line right after the function call. The Anti-Goto Order was so named because I believe that GOTO should be learned and used but once you learn about GOSUB and/or functions you need to leave GOTO in the past... hence the acronym AGO meaning in the past. I don't preach 24/7 against the use of GOTO but you'll often catch me de-GOTOing newbie code.

Quote: "I program on a system that doesn't have loops and only has a goto command. It allows for freer, faster and shorter coding. Also, if you use bookmarkers, it is actually easier to understand code that uses goto statements because instead of just seeing an end bracket, you tell what is the end of a loop and where it will jump."


Now with a language that has no loops (not even FOR/NEXT?) I understand you have to use GOTO but thankfully Darkbasic isn't one of them. What's that language called?

Login to post a reply

Server time is: 2024-09-29 00:30:14
Your offset time is: 2024-09-29 00:30:14