BleepingComputer.com: Saving to text code

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Saving to text code

#1 User is offline   TH0RN 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 28
  • Joined: 02-October 08
  • Gender:Male

Posted 14 October 2008 - 03:23 AM

I am trying to make an application that can look through directories and save certain files as favorites. The problem is that every time I change the favourite file, it says that it is a bad file, or that it is already open.

This is the code that i already have.

Private Sub cmd10_Click()
Open App.Path & ("\Choice10.Txt") For Output As #1
Write #1, txtAdd.Text
Close #1
Open App.Path & ("\Choice010.Txt") For Output As #1
Write #1, txtaddcaption.Text
Close #1
Open App.Path & "\Choice10.txt" For Input As #1
Input #1, ch10
Close #1
Open App.Path & "\Choice010.txt" For Input As #1
Input #1, ch010
Close #1
End Sub

what should I do to fix this?

#2 User is offline   Keithuk 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 800
  • Joined: 25-January 07
  • Gender:Male

Posted 14 October 2008 - 07:15 AM

View PostTH0RN, on Oct 14 2008, 09:23 AM, said:

I am trying to make an application that can look through directories and save certain files as favorites. The problem is that every time I change the favourite file, it says that it is a bad file, or that it is already open.

This is the code that i already have.

Private Sub cmd10_Click()
Open App.Path & ("\Choice10.Txt") For Output As #1
Write #1, txtAdd.Text
Close #1
Open App.Path & ("\Choice010.Txt") For Output As #1
Write #1, txtaddcaption.Text
Close #1
Open App.Path & "\Choice10.txt" For Input As #1
Input #1, ch10
Close #1
Open App.Path & "\Choice010.txt" For Input As #1
Input #1, ch010
Close #1
End Sub

what should I do to fix this?


Open App.Path & ("\Choice10.Txt") For Output As #1

You don't need the () around the filename but the last two files you haven't used them. The file shouldn't be already open because your using Close #1 after you write to the file unless another part of your app as them open. Looking at that code apart from the brackets it looks ok. This must mean you have these files open somewhere else in your app thats why you get the bad file or the file is already open.

What you could use when you write to multiple files is

Dim FileNum As Integer

FileNum = Freefile()
'Freefile atticates the next available number to FileNum variable so they are being used by another process.

Open App.Path & "\Choice010.txt" For Input As #FileNum
Input #FileNum, ch010
Close #FileNum

Are all these files being written to in one Command button press? If you want to make sure that all open files are closed just use Close that will close all files that your app has open before you try to open another one.

Now the other problem you are to have with this is when you write text to the file it will overwrite the text thats already in that file. If you want to add the text to the end of that file then use Append.

Open App.Path & "\Choice10.Txt" For Append As #FileNum :thumbsup:
Keith

Martin2k

Windows ME (spare computer)
Windows XP 2002 Professional SP3 (desktop computer)
Windows Vista Home Premium SP2 (laptop computer)

#3 User is offline   TH0RN 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 28
  • Joined: 02-October 08
  • Gender:Male

Posted 16 October 2008 - 03:08 AM

Thanks :thumbsup:

#4 User is offline   Keithuk 

  • Distinguished Member
  • PipPipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 800
  • Joined: 25-January 07
  • Gender:Male

Posted 17 October 2008 - 06:11 AM

View PostTH0RN, on Oct 16 2008, 09:08 AM, said:

Thanks :thumbsup:


Ok so where was the fault? :flowers:
Keith

Martin2k

Windows ME (spare computer)
Windows XP 2002 Professional SP3 (desktop computer)
Windows Vista Home Premium SP2 (laptop computer)

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users