File editing without changing structure


 File editing without changing structure
       
 
 
 Hi,

   I am trying to open and edit a tab delimited file. I need to replace one column header with a token. I am able to open and edit the file, however, the file structure ends up changing.

For example, if I have a file "data.txt" with a column name "Email" on the first line and I want to replace this with a standard token "Your Email"

I can read the line and use a regular expression to replace the column name with the token. Now when I try to write the line back into the same file using the WriteLine method of a streamwriter it overwrites part of the second line of "data.txt"

Is there a way I can go into files and edit parts of it without chainging the file. I know it can be done in Java, but is it possible in C##

I dont want to create a new file, only edit the current file I have

Thanks

Umer

For reference this is what I am doing

 

try

{


sr = new StreamReader(filename);


//create a StreamReader to read file

String line;


line = sr.ReadLine();

//read first line of the file

if(sr!=null)

sr.Close();


sb = new FileStream(filename, FileMode.OpenOrCreate);

//create a new file stream with Open or Create access

sw = new StreamWriter(sb);

int k = token.Length;

 // If you just use the StreamWriter without wrapping it in FileStream you overwrite the file.

// It does not replace the first line. FileMode.OpenOrCreate lets you open the file and write to it.

if(token.Length<email.Length)

{

for(int i=0;i<email.Length - k ;i++)

{

token = token + " ";

}



string line1 = Regex.Replace(line, email, token, RegexOptions.Compiled|RegexOptions.Singleline|RegexOptions.IgnoreCase);

//replace occurence of email with desired token case insensitive

sw.WriteLine(line1);

//Write to the file

 

 

if(sw!=null)

sw.Close();

if(sb!=null)

sb.Close();



}




catch (Exception e)

{

System.Diagnostics.Debug.WriteLine(e.Message);

}

finally

{

if(sr!=null)

sr.Close();

if(sw!=null)

sw.Close();

if(sb!=null)

sb.Close();


}

 

 
 
 
kusanagiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
If you don't want to use a secondary file then you need to store the ENTIRE new file in memory and then overwrite the file with the changes all at once.

Read the source file line by line and store each changed line in a StringBuilder for instance.  Then close the file and reopen it in write mode.  Now write out the changed lines from StringBuilder all at once, overwriting what was already there.
kusanagiAuthor Commented:
I am trying to save the time it takes to overwrite the whole file. What you said is my current solution.
However, I will be looking at very large files and I will always have to make a change to only the first line.
 I don't want to resort to overwriting the whole file if I have an alternate.
I know there are utilities in Unix to do this but I want to do this in C##
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Well...

You said you want to change "Email" to "Your Email".  This means that all the rest of the data beyond that point in the file would have to SHIFT to the right to make space for the extra characters "Your ".  You can't do this in place with file operations...that just isn't how files work.  =\

If you were changing the data to something else that had the same length (thus no shifting), then it would be possible to move the file pointer and simply overwrite the data that is already there.

If you are dealing with very large files then I don't think you should keep the entire file in memory either.

A secondary file makes the most sense here.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.