Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Editing a text file

Editing a text file

Scheduled Pinned Locked Moved C#
helptutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    analytiks
    wrote on last edited by
    #1

    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!!!!!!!!!

    I K 2 Replies Last reply
    0
    • A analytiks

      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!!!!!!!!!

      I Offline
      I Offline
      Ingo
      wrote on last edited by
      #2

      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!

      A 1 Reply Last reply
      0
      • A analytiks

        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!!!!!!!!!

        K Offline
        K Offline
        Koushik Biswas
        wrote on last edited by
        #3

        /* 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

        1 Reply Last reply
        0
        • I Ingo

          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!

          A Offline
          A Offline
          analytiks
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups