11 Replies - 468 Views - Last Post: 12 July 2012 - 08:19 AM Rate Topic: -----

#1 justinman110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-July 12

problem with input list of text from a text document

Posted 10 July 2012 - 09:40 AM

I have the code to save it in a list. But I am trying to use the same idea to load the text but it is not working I know it’s possible I have done it once before. Any ideas?
This is the code I am using to save it.

Dim list(5) As String * 10
Dim datafile As String
Dim item As Integer
Cls: Print
'the list of items
list(1) = "A"
list(2) = "D"
list(3) = "C"
list(4) = "D"
list(5) = "E"



datafile = "\\xtho.rbe.sk.ca\student\justin.chapman\File.txt"
'open the file to receive data
Open datafile For Output As 1
For item = 1 To 5
Print #1, list(item)
Next item
Close #1



Is This A Good Question/Topic? 0
  • +

Replies To: problem with input list of text from a text document

#2 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: problem with input list of text from a text document

Posted 10 July 2012 - 09:58 AM

you will need to open the file for Input.
using Do while loop with a condition to know when you reach the end of the file you can input lines into variables.

using your current variable called Item you can increse it every full cycle of the Do loop starting with Item=0 and putting it insted of a number inside the List arry like this:

List(Item)


you have to turn on the Option Base 1 by writing it in the very beggining of the code (where you declare variables) or your current arry List(5) will have in fact 6 cells in it (0 to 5)
Was This Post Helpful? 0
  • +
  • -

#3 justinman110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-July 12

Re: problem with input list of text from a text document

Posted 10 July 2012 - 10:26 AM

i still get am error "bad file mode" on the
Print #1,

can u post what you have mabey i still have something wrong.
Was This Post Helpful? 0
  • +
  • -

#4 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: problem with input list of text from a text document

Posted 10 July 2012 - 11:08 AM

Open datafile For Output As 1


you forgot the # thats why you got the error


Open datafile For Output As #1

Was This Post Helpful? 0
  • +
  • -

#5 justinman110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-July 12

Re: problem with input list of text from a text document

Posted 11 July 2012 - 08:09 AM

still the same and i added the #.

Print #1, list(item = 0)



Open datafile For Input As #1

Was This Post Helpful? 0
  • +
  • -

#6 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: problem with input list of text from a text document

Posted 11 July 2012 - 09:24 AM

Why are you opening the file for output if you're trying to input stuff into it?
Was This Post Helpful? 0
  • +
  • -

#7 justinman110  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-July 12

Re: problem with input list of text from a text document

Posted 11 July 2012 - 09:54 AM

the code above is used to save text in list form in a notepad and it works. now i am trying to recover the text saved in the document when i load the form.
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: problem with input list of text from a text document

Posted 11 July 2012 - 11:15 AM

Well, why do you show that you have changed the 1 to #1 and also the Output to Input, then? Maybe you should repost your code.
Was This Post Helpful? 0
  • +
  • -

#9 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: problem with input list of text from a text document

Posted 11 July 2012 - 11:48 AM

you should set the variable item to zero at the Form_Load event.

before you start the loop you need to open the file for Input as #1 (if you want to use number 1)
a good practice is to make a variable that is used as file number and before the loop make it Variable = FreeFile this way even if you forget to close the file you can never open another one with an already taken number.

use a Do While loop with condition to exit loop if reaching the end of the file.
inside the loop input every line into List.

the variable item is the index for the List arry there for when inputing into List it would look this way:
Line Input #1,List(item)


---
NOTICE: List is static arry, unless using Option Base 1 List will have 6 and only 6 slots in it since you declared is as
Dim List(5) As String*10
(btw *10 mean that each member cannot be longer than 10 characters, that will cut off longer lines and would result in data being lost)

(if using Option Base 1 List will have only 5 slots in it)

you should consider making List as dynamic arry by declaring it like this:
Dim List() As String*10
with that you can choose how many slots you want List to have during run-time without having to change the code and recompile the program.

you will however need nother variable and another loop to count how many lines the file have and then use this:
ReDim List(NumOfLines) As String*10


---

at the very end of the loop you use to actually read and put data into List you need to increse the variable item
item = item + 1


i hope this help :)
Was This Post Helpful? 0
  • +
  • -

#10 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: problem with input list of text from a text document

Posted 11 July 2012 - 07:53 PM

Neku, if I understand your code correctly you can redim inside the loop (using redim preserve) each time using Ubound(List) + 1 as the upper bound. This way you avoid having to loop through the file before looping through the array.
Was This Post Helpful? 1
  • +
  • -

#11 Neku  Icon User is offline

  • D.I.C Regular

Reputation: 18
  • View blog
  • Posts: 255
  • Joined: 21-May 09

Re: problem with input list of text from a text document

Posted 12 July 2012 - 04:33 AM

well.. thats new.. wouldent it delete the data in the variable?
Was This Post Helpful? 0
  • +
  • -

#12 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: problem with input list of text from a text document

Posted 12 July 2012 - 08:19 AM

That's the point of the Preserve keyword. It will if you don't use it, it will preserve the data if you do. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1