edittin lines of text file by vb2005

edittin lines of text file by vb2005

Page 1 of 1

4 Replies - 1830 Views - Last Post: 16 August 2009 - 01:10 AM Rate Topic: -----

#1 hadi_hashemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-August 09

edittin lines of text file by vb2005

Post icon  Posted 15 August 2009 - 09:35 AM

Hello
I use vb2005

my problem is in access to lines of a text file .
I can write my text data line by one using below code . but i couldnt write to a specific line(like line 5) or else read a
specific line . and also clear one line and rewrite new string.

I searched many . but I couldntt get result related to this problem.
plz help me
thank you

Dim objReader As New System.IO.StreamReader(C:\test.txt,true)
Dim objWriter As New System.IO.StreamWriter(C:\test.txt, True)
objWriter.WriteLine(my text)
objWriter.readLine(my text)



Is This A Good Question/Topic? 0
  • +

Replies To: edittin lines of text file by vb2005

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: edittin lines of text file by vb2005

Posted 15 August 2009 - 09:46 AM

To read a certain line from a text file is relatively easy, first convert the lines in the file to an array then select the line you want. Here's an example:

''' <summary>
''' Method to read a specified line in a text file
''' </summary>
''' <param name="file">File to read from</param>
''' <param name="line">Line number to read</param>
''' <returns></returns>
Public Shared Function ReadSpecifiedLine(file As String, line As Integer) As String
	'create a variable to hold the contents of the file
	Dim fileContents As String = String.Empty
	'create a variable to hold our line contents
	Dim lineContent As String = String.Empty
	'always use a try...catch to deal
	'with any exceptions that may occur
	Try
		Using stream As New StreamReader(file)
			fileContents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
			Dim linesArray As String() = fileContents.Split(New Char() {ControlChars.Lf})
			lineContent = linesArray(line)
			Return lineContent
		End Using
	Catch ex As Exception
		'deal with any errors
		Console.WriteLine(ex.Message)
		Return lineContent
	End Try
End Function



That should get you started :)
Was This Post Helpful? 1
  • +
  • -

#3 hadi_hashemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-August 09

Re: edittin lines of text file by vb2005

Posted 15 August 2009 - 12:09 PM

thank you for preceding help

I proceeded to use the below segment of your code to write string(i)
to line(n) inside of a routine .
but after executing it, this error displayed(Index was outside the bounds of the array) .
I understood that this error was for the reason that there wasnt any string in lines . I wrote the lines manually random strings.
but after executing , I dont see any changes in text file . meaning that
linesArray(n) = i doesnt affect .

Using stream As New System.IO.StreamReader(FILE_NAME)

			fileContents = stream.ReadToEnd().Replace(vbCr & vbLf, vbLf).Replace(vbLf & vbCr, vbLf)
			Dim linesArray As String() = fileContents.Split(New Char() {ControlChars.Lf})
			linesArray(n) = i

		End Using



what should I do
thanks

Mentor Edit: :code:

This post has been edited by hadi_hashemi: 15 August 2009 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: edittin lines of text file by vb2005

Posted 15 August 2009 - 04:29 PM

There will be no changes in the output file until you write it out. Also what are the values of n and i - If n >= the number of lines in the array, it will give the error. remember that a 12 line array is numbered 0..11. I bet that n is larger than the number of lines in the array. You cannot just write to the 10th element of a 4 element array.
Was This Post Helpful? 0
  • +
  • -

#5 hadi_hashemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 15-August 09

Re: edittin lines of text file by vb2005

Posted 16 August 2009 - 01:10 AM

ok :rolleyes:
I almost solved the problem to write a specific line uiseng this sub:
	
Private Sub dataset()

		Dim fileText As String = My.Computer.FileSystem.ReadAllText(FILE_NAME)
		linesArray = fileText.Split(vbNewLine)
		linesArray(x) = i

		Dim sw As New System.IO.StreamWriter(FILE_NAME)

		For t = 0 To 100
			sw.WriteLine(linesArray(t))
		Next
		sw.Close()


	End Sub



which x is the number of the line that I want to write .and the i is content to be written.

now my problem :

I have declared linesArray in 100 members :
Dim linesArray(100) As String

but after executing program an error occured in (linesArray(x) = i)
it says:Index was outside the bounds of the array

because (test.txt) is empty of any string content.

in this error vb shows me that linesArray hasnt any member.
but I have declared 100 members for it.

is the resolution that i allocate initial strings to "test.txt"
if i do so there will not be problem .
but it doesnt seem technichally to resolve so ...

This post has been edited by hadi_hashemi: 16 August 2009 - 11:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1