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. Urgent - Need help inserting line into text file.

Urgent - Need help inserting line into text file.

Scheduled Pinned Locked Moved C#
helpdebugging
5 Posts 4 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.
  • I Offline
    I Offline
    IrishSonic
    wrote on last edited by
    #1

    Hi I have a text file with the following layout: [PROFILE1] SYSTEM=home DEBUG=true [PROFILE2] SYSTEM=laptop DEBUG=true What I want to do is insert a new line after the FIRST DEBUG LINE in PROFILE1 so the whole of PROFILE2 then moves down a line. My problem is that I keep overwriting the start of PROFILE2 e.g [PROFILE1] SYSTEM=home DEBUG=true testline 2] SYSTEM=laptop DEBUG=true Can you take a look at my code and see what I am doing wrong!! if( File.Exists( "d:\\networld.ini" ) ) { long lFilePos = 0; string sLine = null; FileStream fs = new FileStream("d:\\networld.ini", FileMode.Open, FileAccess.ReadWrite); // Create a new streams to read and write to the file StreamReader sr = new StreamReader( fs ); StreamWriter sw = new StreamWriter( fs ); while( sr.Peek() > -1 ) { sLine = sr.ReadLine(); if( sLine.CompareTo( "[" + "PROFILE1" + "]" ) == 0 ) { lFilePos = sLine.Length; while( sr.Peek() > -1 ) { sLine = sr.ReadLine(); lFilePos += sLine.Length + 2; if( sLine.CompareTo( "" ) == 0 ) { fs.Seek( lFilePos,SeekOrigin.Begin ); sw.Write( "testline\r\n" ); sw.Flush(); break; } } } } sr.Close(); fs.Close(); MessageBox.Show( "Finished Writing To File." ); } Thanks a million.

    D L 2 Replies Last reply
    0
    • I IrishSonic

      Hi I have a text file with the following layout: [PROFILE1] SYSTEM=home DEBUG=true [PROFILE2] SYSTEM=laptop DEBUG=true What I want to do is insert a new line after the FIRST DEBUG LINE in PROFILE1 so the whole of PROFILE2 then moves down a line. My problem is that I keep overwriting the start of PROFILE2 e.g [PROFILE1] SYSTEM=home DEBUG=true testline 2] SYSTEM=laptop DEBUG=true Can you take a look at my code and see what I am doing wrong!! if( File.Exists( "d:\\networld.ini" ) ) { long lFilePos = 0; string sLine = null; FileStream fs = new FileStream("d:\\networld.ini", FileMode.Open, FileAccess.ReadWrite); // Create a new streams to read and write to the file StreamReader sr = new StreamReader( fs ); StreamWriter sw = new StreamWriter( fs ); while( sr.Peek() > -1 ) { sLine = sr.ReadLine(); if( sLine.CompareTo( "[" + "PROFILE1" + "]" ) == 0 ) { lFilePos = sLine.Length; while( sr.Peek() > -1 ) { sLine = sr.ReadLine(); lFilePos += sLine.Length + 2; if( sLine.CompareTo( "" ) == 0 ) { fs.Seek( lFilePos,SeekOrigin.Begin ); sw.Write( "testline\r\n" ); sw.Flush(); break; } } } } sr.Close(); fs.Close(); MessageBox.Show( "Finished Writing To File." ); } Thanks a million.

      D Offline
      D Offline
      DavidR_r
      wrote on last edited by
      #2

      Why would not you consider the old fashion way read the old file and create simultaneously a new file -> copy each line while they are identical, then insert the change and so on. When you finish you can delete the old file and rename the new file to the old name. You can use File class. I think it will save you much trouble DavidR

      1 Reply Last reply
      0
      • I IrishSonic

        Hi I have a text file with the following layout: [PROFILE1] SYSTEM=home DEBUG=true [PROFILE2] SYSTEM=laptop DEBUG=true What I want to do is insert a new line after the FIRST DEBUG LINE in PROFILE1 so the whole of PROFILE2 then moves down a line. My problem is that I keep overwriting the start of PROFILE2 e.g [PROFILE1] SYSTEM=home DEBUG=true testline 2] SYSTEM=laptop DEBUG=true Can you take a look at my code and see what I am doing wrong!! if( File.Exists( "d:\\networld.ini" ) ) { long lFilePos = 0; string sLine = null; FileStream fs = new FileStream("d:\\networld.ini", FileMode.Open, FileAccess.ReadWrite); // Create a new streams to read and write to the file StreamReader sr = new StreamReader( fs ); StreamWriter sw = new StreamWriter( fs ); while( sr.Peek() > -1 ) { sLine = sr.ReadLine(); if( sLine.CompareTo( "[" + "PROFILE1" + "]" ) == 0 ) { lFilePos = sLine.Length; while( sr.Peek() > -1 ) { sLine = sr.ReadLine(); lFilePos += sLine.Length + 2; if( sLine.CompareTo( "" ) == 0 ) { fs.Seek( lFilePos,SeekOrigin.Begin ); sw.Write( "testline\r\n" ); sw.Flush(); break; } } } } sr.Close(); fs.Close(); MessageBox.Show( "Finished Writing To File." ); } Thanks a million.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        IrishSonic wrote: Can you take a look at my code and see what I am doing wrong!! You are doing nothing, you are just using it wrong. Best would be to read the file into a stringbuilder instance and just insert the text, then just rewrite the file. There is no point in doing it directly in the file, because your data cannot be "shifted" forward. Imagine trying this (what u wanna do) on a 1Gbyte file.... :) top secret xacc-ide 0.0.1

        I 1 Reply Last reply
        0
        • L leppie

          IrishSonic wrote: Can you take a look at my code and see what I am doing wrong!! You are doing nothing, you are just using it wrong. Best would be to read the file into a stringbuilder instance and just insert the text, then just rewrite the file. There is no point in doing it directly in the file, because your data cannot be "shifted" forward. Imagine trying this (what u wanna do) on a 1Gbyte file.... :) top secret xacc-ide 0.0.1

          I Offline
          I Offline
          IrishSonic
          wrote on last edited by
          #4

          Ok guys, comments definitely taken on board!! But can't believe that if you want to insert a new line at a specific point, it means rewriting the file to a new file and deleting the old one. Is this the industry standard way its always done?? So what if I wanted to just update a line?? Would I still have to write to a new file and delete old?? Also could I trouble you for maybe a quick example of placing everything into a StringBuilder with the new line. Thanks.

          S 1 Reply Last reply
          0
          • I IrishSonic

            Ok guys, comments definitely taken on board!! But can't believe that if you want to insert a new line at a specific point, it means rewriting the file to a new file and deleting the old one. Is this the industry standard way its always done?? So what if I wanted to just update a line?? Would I still have to write to a new file and delete old?? Also could I trouble you for maybe a quick example of placing everything into a StringBuilder with the new line. Thanks.

            S Offline
            S Offline
            Sue92abc
            wrote on last edited by
            #5

            Did you ever figure this out? I need to edit a line in a text file and I do NOT want to write everything to a new file to do this. Any help would be appreciated! Sue

            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