Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 21st, 2008, 08:18 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Read and Edit the text file

Hi,
I am creating an application through which I can read a text file saved on my disk and find the block of strings through specified array of spans (start and end positions of strings to find) then after I need to replace these blocks of string with other block of string and create a new text file so that at the end I can have 2 text files old one and the modified new one.

I am explaining this with an example:--
lets say I have a text file old.txt which contains following data in it:
Find the formula to calculate area of a circle.
Find the formula to calculate area of a square.
Find the formula to calculate area of a rectangle.
Find the formula to calculate area of a tringle.
and so on..

now I need to modify this text file with following new data and create the new.txt file for which I will pass the span values of "formula" like start value=9 and end value=16 for 1st sentence and so on..:
Find the <Math> formula <Equation> to calculate area of a circle.
Find the <Math> formula <Equation> to calculate area of a square.
Find the <Math> formula <Equation> to calculate area of a rectangle.
Find the <Math> formula <Equation> to calculate area of a tringle.


Can anyone help me to acheive this.

Thanks in advance.
  #2 (permalink)  
Old February 21st, 2008, 08:39 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

If you read the text from the file into an array of strings (e.g. File.ReadAllLines() method), then you just need to do two Insert commands. Use a StringBuilder for your output text.

string[] lines = File.ReadAllLines("C:\\test.txt");

int pos1 = 9;
string insert1 = "<Math>";
int pos2 = 16 + insert1.Length;
string insert2 = "<Equation>";

StringBuilder output = new StringBuilder();

foreach(string s in lines)
{
    string t = s.Insert(pos1, insert1);
    output.Append(t.Insert(pos2, insert2));
}

// now write out output.ToString()

/- Sam Judson : Wrox Technical Editor -/
  #3 (permalink)  
Old February 26th, 2008, 03:25 AM
Registered User
 
Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your guidance..
but this approach will not be good if the size of text file is big.
And according to your aproach start and end span is always 9 and 16 respectively but this is not the case, for second sentence span values are 57 and 64 respectively and so on..

Thanks..
  #4 (permalink)  
Old February 26th, 2008, 05:35 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

My method splits the text file into a number of lines - you are only counting from the start of the line, not from the start of the text file. Therefore assuming your example is a valid example it would be the 9th and 16th characters on each and every line.

/- Sam Judson : Wrox Technical Editor -/


Similar Threads
Thread Thread Starter Forum Replies Last Post
read text file senthilreddy ASP.NET 1.0 and 1.1 Basics 1 December 19th, 2010 10:56 PM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
How to read a line from a text file ? nobitavn94 C# 3 November 25th, 2006 11:04 PM
Read all rows from text file shai505 C# 5 April 28th, 2006 09:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.