Editing a text file
-
Is there any way to edit a text file without using an intermediate temporary file. The text file has one entry in each line, and if there are 90 entries i.e. 90 lines, and I need to edit some lines in between (i.e say fro eg the 4th and the 30th entried) without creating an intermediate file. I tried using the FileStream class, it writes only in bytes, and I am stuck as to how to increase the seek pointer linewise. My code goes like this System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); Byte[] b = encoding.GetBytes("\r\n New Entry \r\n"); fs.Seek(1,SeekOrigin.Current); // fs is the FileStream fs.Write(b,0,c.Length); The problem here is in the Seek function, as I need to increment the seek pointer to point to the start of the next line that I am supposed to edit. The above solution is affecting two lines. The required line gets edited but the entry in the next line is replaced by a blank line. And if the '\r\n' is not given then the text is appended to the same line. Any solutions, Please HELP!!!!!!!!!
-
Is there any way to edit a text file without using an intermediate temporary file. The text file has one entry in each line, and if there are 90 entries i.e. 90 lines, and I need to edit some lines in between (i.e say fro eg the 4th and the 30th entried) without creating an intermediate file. I tried using the FileStream class, it writes only in bytes, and I am stuck as to how to increase the seek pointer linewise. My code goes like this System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); Byte[] b = encoding.GetBytes("\r\n New Entry \r\n"); fs.Seek(1,SeekOrigin.Current); // fs is the FileStream fs.Write(b,0,c.Length); The problem here is in the Seek function, as I need to increment the seek pointer to point to the start of the next line that I am supposed to edit. The above solution is affecting two lines. The required line gets edited but the entry in the next line is replaced by a blank line. And if the '\r\n' is not given then the text is appended to the same line. Any solutions, Please HELP!!!!!!!!!
-
Is there any way to edit a text file without using an intermediate temporary file. The text file has one entry in each line, and if there are 90 entries i.e. 90 lines, and I need to edit some lines in between (i.e say fro eg the 4th and the 30th entried) without creating an intermediate file. I tried using the FileStream class, it writes only in bytes, and I am stuck as to how to increase the seek pointer linewise. My code goes like this System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding(); Byte[] b = encoding.GetBytes("\r\n New Entry \r\n"); fs.Seek(1,SeekOrigin.Current); // fs is the FileStream fs.Write(b,0,c.Length); The problem here is in the Seek function, as I need to increment the seek pointer to point to the start of the next line that I am supposed to edit. The above solution is affecting two lines. The required line gets edited but the entry in the next line is replaced by a blank line. And if the '\r\n' is not given then the text is appended to the same line. Any solutions, Please HELP!!!!!!!!!
/* First read your file into memory, 1 line per arraylist member */ ArrayList alLines = new ArrayList(); StreamReader sr = new StreamReader( "C:\\myfile.ext" ); string szLine = sr.ReadLine(); while( szLine != null ) { alLines.Add( szLine ); szLine = sr.ReadLine(); } sr.Close(); /* Done reading the file in memory. Now make all * the changes you want to make */ alLines[ 4 ] = "BLAH BLAH"; alLines[ 30 ] = "MORE BLAH BLAH"; alLines[ 56 ] = alLines[ 56 ].ToString() + " Appended string"; /* Write it back to the same file, overwriting */ StreamWriter sw = new StreamWriter( "C:\\myfile.ext" ); for( int iCnt = 0; iCnt < alLines.Count; iCnt++ ) sw.WriteLine( alLines[ iCnt ].ToString() ); sw.Close();
:) Koushik Biswas -- modified at 16:33 Thursday 2nd March, 2006 -
Why don't you work in the storage? Read the file you want to change, change it and then write it back? Greetings, Ingo ------------------------------ A bug in a Microsoft Product? No! It's not a bug it's an undocumented feature!
Hi, The problem is that i'm doing this for a Pocket PC with a memory capacity of 64 MB and the file can be quite large. Reading all of the data into storage can sloq down the processing speed considerably. This is quite a dilemma i'm in :) . Help me out,Guys.