How to delete a line from a text file using C#
Hello all
I am developing a little app where I need to monitor a file that is update by several machines.
I need to gather each line wrote by each machine so I need access to a specific line number and take that line, store it into a DB table and delete that line.
I really don't know how to delete the line without parse the information into a string variable and rewrite the file. This can lake the app because the file can contain up to 1000K lines... :-O...
Any suggestion will be appreciated.
__
I think I deleted the Internet...
[552 byte] By [
DarthVic] at [2007-12-16]
When you delete a line in a text editor like Notepad, that's literally what it does--it rewrites the whole file when you save. There isn't a better way to do it since conventional text files must have all the bytes after the deleted line to be moved forward, filling in the gap.
The best solution is to reconfigure the logging machines to write directly to the database. ISA and IIS both support ODBC, for instance. If you can go this route, and use SQL Server (or another robust DB system), you can use a trigger to intercept the raw logging rows and normalize it to save space and improve query performance.
The second-best solution is to configure the logging machines to create a new text file at intervales (like daily). Then, after your tool processes the log, you can delete the file.
-Ryan / Kardax
Hi Ryan
Yes, I think close like you at the first solution... the problem is that the client machines are using OS/2 Warp 4 and Rexx programming language and I spent too many time trying to find a way to connect the Application to SQL Server 2K with no results...
But following with the main thread, then I just need a way to access a specific line number from the text file and I can easily skip that line and re-write then the whole file.
If It can not be possible then I would need to re-take Rexx programming...
...I hope no for my good enough
Since you are accessing by line number, why delete the line at all?
The file is update every 15 seconds by multiple machines and that line must be stored in a SQL Server Database, so I need remember the line number that the App is managing to send the correct info.
It is no necesary to delete the line but just for performance is recomended do it once the file can contain up to 1000K lines.
_
I think I deleted the Internet
Question. Are you storing all the lines in the database or just select lines? If you just store select lines, do you need to keep the other lines around? If you give us a little more information, I'll bet we can come up with something that will work well for you.
In fact, I store ALL lines except for the header (first three lines).
Once the data is stored I can drop it away.
__
I think I deleted the internet
OK, How do the apps that write to the log file behave if...
a) The file is missing.
b) The file is locked.
The apps append the file, so:
a) If the file is missing, the apps create a new one.
b) if the file is locked, wait ~10 seconds and if still locked, then fail.
Cool! That makes it easy!
Step 1: Rename the file to some temporary name. (This makes it yours!)
Step 2: Using StreamReader, walk the file and insert the lines into the database. Since there are so many lines, you will probably want to do bulk inserts.
Step 3: Delete the temporary file.
Ok. I got it.
Now, how can I walk the file starting from the line number 3? (skipping the header)
I have some code, but the program seems to hang up a lot.
take a look on it.
| | string myFile=""; string fileName = @path; StreamReader srRead = File.OpenText(fileName); srRead.BaseStream.Seek(0, SeekOrigin.Current); //what does SeekOrigin.Current do? while (srRead.Peek() > -1) { myFile = myFile + srRead.ReadLine() + "\n"; } srRead.Close(); //Split the file into an array using a newline character as the split character char[] splitChar = {'\n'}; string [] fileArray; fileArray = myFile.Split(splitChar); //Clear the old file contents - //For better performance use a StringBuilder //object. myFile=""; //iterate over the Array which contains each line of the file for(int x=0;x<fileArray.Length;x++) { //Do not store the first three lines of the file if(x > 3) { myFile = myFile + fileArray[x].ToString(); } } //Delete the file File.Delete(fileName); //Write the new file FileStream fs = new FileStream(fileName,FileMode.OpenOrCreate, FileAccess.Write); StreamWriter swFromFileTrueUTF8Buffer = new StreamWriter(fs, System.Text.Encoding.UTF8); swFromFileTrueUTF8Buffer.Write(myFile); swFromFileTrueUTF8Buffer.Close(); fs.Close(); |
While the program reads all file and parse the info into the string I can go to lunch!
If I can read the file and parse the data directly to the DB, I will avoid the double read of info.
Cheers
Well, finally my code stays like this...
This is not what I was looking for but works.
Your ideas were so helpful for me.
See you soon.
| |
static void ReadRepFile() { string myFile=""; //Read the file and put into the //variable "myFile" string fileName = @path; using (StreamReader srRead = new StreamReader(fileName)) { // I skip the first three lines. for( int z = 0;z<3;z++) { srRead.ReadLine(); } while ((myFile = srRead.ReadLine()) != null) { // here goes the code to load info to DB... } srRead.Close(); } //Close the file //Delete the file - you could also backup the file. File.Delete(fileName); }
|
I Think I deleted the Internet...