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