?
Solved

Read text from file

Posted on 2007-08-10
5
Medium Priority
?
211 Views
Last Modified: 2013-11-05
Hi all

I have this code:
Private Sub readjFile()
        Dim pathAs String = "D:\Source\ex.txt"
        If File.Exists(path) Then
            Dim ulaznaLinija As String
            Dim fs As FileStream = New FileStream(putanja, FileMode.Open, FileAccess.Read)
            Dim sr As New StreamReader(fs)
            sr.BaseStream.Seek(0, SeekOrigin.Begin)
            Do While sr.Peek >= -1
                ulaznaLinija = sr.ReadLine
                If Not (ulaznaLinija Is Nothing) Then
                    proces(ulaznaLinija)
                End If
            Loop 'While Not (ulaznaLinija Is Nothing)
            fs.Flush()
            sr.Close()
        End If
    End Sub

when I read this "ulaznaLinija" I want to delete it from the file on  top ("D:\Source\ex.txt")
0
Comment
Question by:amaimedia
  • 3
  • 2
5 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 19668483
Do I understand it correctly ?
- You are reading a file
- In this file you want to search a specific string
- If you find this string, you want it to be removed from the file
0
 

Author Comment

by:amaimedia
ID: 19668526
I read the file line per line, and when I have read a line, I send it to: proces(ulaznaLinija). When tihis proces (ulaznaLinija) has finished I want to delete this line from the file.

 
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 19668553
Why would you delete the line ? If you read the complete file line by line, you can process every line and afterwards just delete the complete file !

Dim pathAs String = "D:\Source\ex.txt"

Dim oRead As System.IO.StreamReader
Dim LineIn As String
' Open your file
oRead = IO.File.OpenText("C:\temp\subparts.txt")
' Read it line by line
While oRead.Peek <> -1
   ' Read a line
   LineIn = oRead.ReadLine()

   Process (LineIn)

End While

oRead.Close()

system.io.file.delete pathAs
0
 

Author Comment

by:amaimedia
ID: 19668597
I can't delete this file, becouse I will need it again. When I have read the whole file I actually want to clear all lines.
0
 
LVL 53

Accepted Solution

by:
Dhaest earned 1500 total points
ID: 19668620
Do you process all the lines and then at the end of your procedure, you'l need an empty file ?

Dim pathAs String = "D:\Source\ex.txt"

Dim oRead As System.IO.StreamReader
Dim LineIn As String
' Open your file
oRead = IO.File.OpenText(pathAs)
' Read it line by line
While oRead.Peek <> -1
   ' Read a line
   LineIn = oRead.ReadLine()

   Process (LineIn)

End While

oRead.Close()

         'In order to write back, we'll need to overwrite the file, so simply delete it here
         IO.File.Delete(pathAs )

         'Create a stream-writer for writing back to the file
         Dim sw As New System.IO.StreamWriter(pathAs )
         sw.Write("")

         'Dispose of the stream-writer - this will also flush the file to disk
         sw.Close()
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question