TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
CI
NA
25
0
File Handling
Jul 12 2004 5:20 AM
Hi, This is what I'm trying to do: 1. Read in an input text file 2. Read in a config text file. Config file has one expression per line. 3 .Compare EVERY expression in config file with ALL text in input file. 4. Every time a match is found in the input file, replace it with something… (eg. for now I’m only replacing all matches with “!REPLACEMENT!”) 5. Write a brand new output file with replaced text instead of the expressions that matched, but all other text remains the same. 6. My code does this for now, but only replaces ALL matches of the FIRST expr in the config file.. (this is because once the input file has been parsed to the end and compared with the first expr, I can’t get it to reinitialize to the beginning of the file) but it needs to replace ALL matches of ALL expressions in the config file The code needs to cycle thru ALL expressions in config file and repeatedly parse the input file and replace ALL OCCURANCES of all of them in the input file. Here's the code: using System; using System.IO; using System.Text.RegularExpressions; namespace fileiochecker { class Class1 { [STAThread] static void Main(string[] args) { String ifile = "data.txt"; // input file String cfile = "config.txt"; // config file String ofile = "newdata.txt"; // output file /* For following code: * i - input; c - config; o - output; * Therefore, isr - input stream reader, csr - config stream reader, * cline - config line, iline - input line */ try { using (StreamReader isr = new StreamReader(ifile)) { String cline,iline; using (StreamReader csr = new StreamReader(cfile)) { using (StreamWriter sw = new StreamWriter(ofile)) { while ((cline = csr.ReadLine()) != null) { Regex testExp = new Regex( cline ); string replaceString = "!REPLACEMENT!"; while ((iline = isr.ReadLine()) != null) { //while (sr.Peek() >= 0) ???? if ( testExp.Match( iline ).Success == false ) { // Write text as it is to the file. sw.WriteLine(iline); Console.WriteLine(iline); } else if ( testExp.Match( iline ).Success == true ) { // Replace target text and write to the file. iline = testExp.Replace( iline, replaceString ); sw.WriteLine(iline); Console.WriteLine(iline); } }// end while iline } // end while cline } // end streamwriter } // end using StreamReader csr } // end using StreamReader isr } // End Try block catch (Exception e) { Console.WriteLine("The file could not be read:"); Console.WriteLine(e.Message); } // End catch block } // End main() } //end class1 } // end namespace PLEASE HELP ! Thanks.
Reply
Answers (
4
)
WndProc related
VB.NET WEB FORMS HELP!