

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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersâ€™ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .





February 21st, 2008, 08:18 AM

Registered User


Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


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.

February 21st, 2008, 08:39 AM


Friend of Wrox


Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts


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 /

February 26th, 2008, 03:25 AM

Registered User


Join Date: Feb 2008
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts


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

February 26th, 2008, 05:35 AM


Friend of Wrox


Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts


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 /

Thread Tools 
Search this Thread 


Display Modes 
Linear Mode

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off




