13 Replies - 1707 Views - Last Post: 23 May 2011 - 04:50 PM Rate Topic: -----

#1 auchjame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-March 11

Problem With Writing To Text Files...

Posted 19 May 2011 - 05:30 PM

hey guys,
i am just testing code to see if it will work on my sytem at school... a lot of things do not work here... i hae been assigned to create a program that stores data in textfiles on buttonclick, and is able to read them at a later date. on my network drive, text files are restricted, and i cannot open them. using vb i can create a text file, but i receive an error when i try to write to the file. i am not sure if this is a problem in my code or the network policy doing its designated job. this is the code i used to test that i will be able to complete the first section of the task
Imports System.IO
Public Class Form1


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        System.IO.File.Create("I:\Test.txt")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim objWriter As New System.IO.StreamWriter("I:\Test.txt")
        objWriter.Write("Testing")
    End Sub
End Class
when i run the code, it will create the file. i receive this error when i try to write to the file.

Mod Edit:
:code:

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Problem With Writing To Text Files...

#2 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 308
  • Joined: 30-January 11

Re: Problem With Writing To Text Files...

Posted 19 May 2011 - 05:56 PM

Notice that File.Create() is a function that returns a FileStream and take a look at a 'Using' block.

The error should hint that the other process has something to do with code in button1 handler

If that does not help or need more detail please reply back
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9206
  • View blog
  • Posts: 34,588
  • Joined: 12-June 08

Re: Problem With Writing To Text Files...

Posted 19 May 2011 - 05:58 PM

You know - stream writer can create a fill if it's not there, right?
Was This Post Helpful? 0
  • +
  • -

#4 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Problem With Writing To Text Files...

Posted 20 May 2011 - 12:43 AM

Are you using Visual Studio 2010?
You do not need to create an instance of objWriter
You should use:

Dim writeFile As IO.StreamWriter
writeFile = IO.File.CreateText("books.txt") 'Creates a new file
writeFile.writeline("your string")
writeFile.Close()


If it gives you an error on your streamwriter declration, you may need to import IO.streamwriter.

Imports IO.StreamWriter 'above class declaration


Was This Post Helpful? 1
  • +
  • -

#5 auchjame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-March 11

Re: Problem With Writing To Text Files...

Posted 22 May 2011 - 05:40 PM

thanks btu, your post was super helpful. my program now writes data to a text file, and i am currently writing the code for the second form to read the data from the text file written in the orginal form. going along the same streamwriter line, i have looked into a streamreader. the code you suggested, writes data to a new line for each writeline command. do you know how to do the same for a readline command? so if i wanted to read the fifth line i would put the readline command after four other commands.
Thankyou so much for all your help. :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#6 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:18 PM

View Postauchjame, on 22 May 2011 - 05:40 PM, said:

thanks btu, your post was super helpful. my program now writes data to a text file, and i am currently writing the code for the second form to read the data from the text file written in the orginal form. going along the same streamwriter line, i have looked into a streamreader. the code you suggested, writes data to a new line for each writeline command. do you know how to do the same for a readline command? so if i wanted to read the fifth line i would put the readline command after four other commands.
Thankyou so much for all your help. :bigsmile:

Readline works the same as writeline. If you want the 5th line you would have to loop through to it, very simple though.
Was This Post Helpful? 1
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:25 PM

Look at this documentation, it may help.
Also the stream readerclass
Was This Post Helpful? 0
  • +
  • -

#8 auchjame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-March 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:34 PM

how do i set it to open the text file i created using the streamwiter earlier? i have tried using this code to test the principles once again,
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim writeFile As IO.StreamWriter
        writeFile = IO.File.CreateText("I:\books.txt") 'Creates a new file  
        writeFile.WriteLine("your string")
        writeFile.Close()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim readfile As IO.StreamReader
        readfile = IO.File.Open("I:\books.txt")
        readfile.ReadLine()
        readfile.Close()
    End Sub
End Class


i receive an error saying
Error 1 Overload resolution failed because no accessible 'Open' accepts this number of arguments. C:\Documents and Settings\spa101\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb 15 20 WindowsApplication1

how do i set it to read Books.text on my I:\ and how do i set it to save lines to a variable?
Thankyou so much for your help :D
Was This Post Helpful? 0
  • +
  • -

#9 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:38 PM

you're on the right track, just change this part:
Dim readfile As IO.StreamReader
readfile = IO.File.OpenText("I:\books.txt") 'from File.Open to File.OpenText
readfile.ReadLine()
readfile.Close()



youll need to also put a Loop around it to check for EOF marker. Or else it will blow up. Try something like this:
Do Until readfile.peek = -1


This post has been edited by Btu: 23 May 2011 - 04:40 PM

Was This Post Helpful? 1
  • +
  • -

#10 auchjame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-March 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:42 PM

so i fixed the open problem. i think.... :/
readfile = IO.File.OpenText("I:\books.txt")


thanks... :D
now how do i make it say line one is equal to say for example label1?

 
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim readfile As IO.StreamReader
        readfile = IO.File.OpenText("I:\books.txt")
        Do Until readfile.Peek = -1
        readfile.ReadLine()
        Loop
        readfile.Close()
    End Sub


like this?
Was This Post Helpful? 0
  • +
  • -

#11 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:46 PM

If you want to compare what it's reading, store the contents in a variable and compare them.

Dim StringVar as String
StringVar = readFile.Readline()
if StringVar = label.text then
'do something
end if



Not sure how you are with string functions, but you can use a .ToUpper or .ToLower to bypass upper or lower case differences.
Was This Post Helpful? 0
  • +
  • -

#12 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:47 PM

Something like:
Dim str As string = readfile.ReadLine()


Look at the documentation above, there are a lot of examples there

This post has been edited by smohd: 23 May 2011 - 04:49 PM

Was This Post Helpful? 1
  • +
  • -

#13 auchjame  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 16-March 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:47 PM

Imports System.IO
Public Class Form1
    Dim lala As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim writeFile As IO.StreamWriter
        writeFile = IO.File.CreateText("I:\books.txt") 'Creates a new file  
        writeFile.WriteLine("your string")
        writeFile.Close()

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim readfile As IO.StreamReader
        readfile = IO.File.OpenText("I:\books.txt")
        lala = readfile.ReadLine()
        Label1.Text = lala
        readfile.Close()
    End Sub
End Class

Thanks Buddy :D i think i have finally figured it out..
Was This Post Helpful? 0
  • +
  • -

#14 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Problem With Writing To Text Files...

Posted 23 May 2011 - 04:50 PM

:tup:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1