tags:

views:

186

answers:

4

I have a text file containing the data in following format

12345 Abdt3 hy45d et45a 76huj agsj7

Now I want to replace hy45d with another sting that is created on run time. But I am not finding any way to do it. It keep replacing the firt item or append it in next line.

Can any one plz help me resolve this issue

Code that I am using is

Public Sub SaveToTextFile(ByVal PNR As String, ByVal CaseCount As Integer, ByVal Type As String)


    '- Location of the Text files
    Dim targetFile As String = "C:\TestReport\TextFiles\PNR.txt"

    '- Open the target file/stream
    Dim sw As StreamWriter = New StreamWriter(targetFile, True)

    ' Try
    If (CaseCount = 0 And Type = 1) Then
        sw.Write(PNR, 0, 6)
    ElseIf (CaseCount = 1 And Type = 1) Then
        sw.WriteLine(PNR, 6, 7)
    ElseIf (CaseCount = 2 And Type = 1) Then
        sw.WriteLine(PNR, 13, 7)
    ElseIf (CaseCount = 3 And Type = 1) Then
        sw.WriteLine(PNR, 20, 7)
    ElseIf (CaseCount = 0 And Type = 2) Then
        sw.WriteLine(PNR, 27, 7)
    ElseIf (CaseCount = 1 And Type = 2) Then
        sw.WriteLine(PNR, 34, 7)
    ElseIf (CaseCount = 2 And Type = 2) Then
        sw.WriteLine(PNR, 41, 7)
    ElseIf (CaseCount = 3 And Type = 2) Then
        sw.WriteLine(PNR, 48, 7)
    ElseIf (CaseCount = 0 And Type = 3) Then
        sw.WriteLine(PNR, 55, 7)
    ElseIf (CaseCount = 1 And Type = 3) Then
        sw.WriteLine(PNR, 62, 7)
    ElseIf (CaseCount = 2 And Type = 3) Then
        sw.WriteLine(PNR, 69, 7)
    ElseIf (CaseCount = 3 And Type = 3) Then
        sw.WriteLine(PNR, 76, 7)
    End If

    'Catch ex As Exception
    '    Console.Write(ex.ToString())
    'End Try

    sw.Close()
End Sub
A: 

brute force way would be to read the whole file, replace any instances of 'hy45d' and replace the (whole) contents of the file with the new string.

pseudocode:

S = readfile;
S = replace(S, "hy45d", "hello");
writeToFile(S);

(I believe the only way to write to files is to append or to replace, I might be wrong though)

EDIT

If you want individual columns and lines you could use a tokenizer per line. (delimiter probably " "). (I am sure there is a String library for vb.net that can either tokenize or split strings, at least this guy/girl seems to have written a few useful string functions http://www.codeproject.com/KB/vb/stringfunctions.aspx )

If you want to do several replacements it might be useful to read it into a 2D Array, replacing a word in column/line would be trivial then, but it could become a memory hog if you have large files.

NomeN
No I want to search by position not by content. I mean in a particular line at a particular column.
so? you can parse strings can't you? just use the same algorithm you would to find the column/line and apply it to the string
NomeN
A: 

There really isn't a "nice" way to do it in the .Net Framework as far as I know. You have pretty much two choices.

1) Read the entire file into memory, do your update, rewrite the file back out to disk.

2) Do the equivalent of reading and writing directly to the physical disk location - which I'm not sure you can do in the Framework.

Is converting the file to something non-sequential (like a database) an option?

David
Actually I need a replication of Excel. As Excel is causing too much trouble on server. So now I want to read and write data in text file like excel.
+1  A: 

There is no good way of doing this. Text files may use encodings that allocate more than one byte for a character. Simply put, even if you know the character position of the target string, you don’t (in general) know its byte position.

The .NET classes therefore reasonably forbid random access on text files. The standard way of coping with this is to read the whole (or at least part) of the text file, change the string, and write the changed string back.

So:

Dim text = My.Computer.FileSystem.ReadAllText("File.txt")
text = text.Replace("hy45d", "myfancynewstring")
My.Computer.FileSystem.WriteAllText("File.txt", text, False)

Short and sweet.

Or, if you want to search by column:

Dim text = My.Computer.FileSystem.ReadAllText("File.txt")
Dim columns = text.Split(" "c)
'' // Replace third column:
columns(2) = "myfancynewstring"
My.Computer.FileSystem.WriteAllText("File.txt", String.Join(" ", columns) , False)
Konrad Rudolph
Notice that all my VB code postings imply `Option Strict On`, `Option Explicit On` and `Option Infer On`, the only reasonable settings for VB.
Konrad Rudolph
Can u plz elaborate your code that is search by column
@sam: I don’t really know what you want. Your question update seems to have nothing to do with the original question. My code is not actually searching anything, since I don’t know what you mean by that, it just updates the third column of text.
Konrad Rudolph
Many Thanks...your solution full fills my requirement..thx
A: 
  1. Search byte position by seek function. 2. Set new cursor stream position by filestream.Seek(bytepos,seekorigin.Begin). 3. Then write or append string to that pos line. And flush it.
surabaya bobo