4
Answers

File Handling

Photo of CI

CI

20y
1.8k
1
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.

Answers (4)

0
Photo of jinwolf
NA 176 0 20y
You're welcome.
0
Photo of CI
NA 25 0 20y
string cline; string iline=""; try { using (StreamReader isr = new StreamReader(ifile)) { iline = isr.ReadToEnd(); 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!"; if (iline != null) { iline = testExp.Replace( iline, replaceString ); Console.WriteLine(iline); // } }// end while iline } // end while cline sw.Write(iline); } // end streamwriter } // end using StreamReader csr } // end using StreamReader isr } // End Try block Thanks to Lee for the solution.
0
Photo of CI
NA 25 0 20y
Lee, thank you for replying. To answer your questions: 1. The config file is just a static text file. It does not change. It just has a few lines, all of which contain some basic expressions like "A0-1234", "X2-765823" etc. I'm sorry but I did not really understand what you recommended in your first point. Could you please clarify ? 2. Even if I read the file into a string or read it to end, how will I be able to compare the entire input text file repeatedly with the search expressions stored in the string ? Thanks.
0
Photo of jinwolf
NA 176 0 20y
Firstly: Does your config file change? if not use App.config and the System.Configuration library. you can retrieve the settings with "ConfigurationSettings.AppSettings[""].ToString();". for a count of the settings use: ConfigurationSettings.AppSettings.Count; Secondly: Read the file into a string and add "\n" after each line is loaded or use read to end. string sInFile = isr.ReadToEnd();
Next Recommended Forum