简体   繁体   中英

Searching for a string in file using c#

I wanna search for a string(function) in a large file and if found, I have to search for another string(signal) and replace it. I have written this code but the signal is getting updated even outside the function. I want to modify the signals that are found only inside this function. Iam beginner for c# and any kind of help would be appreciated.

if (openFileDialog2.FileName.Contains(function))
                                                File.WriteAllText(openFileDialog2.FileName, File.ReadAllText(openFileDialog2.FileName).Replace(signal, replace));
                                            MessageBox.Show("Done");
                                        }

I also tried,

 string contents = File.ReadAllText(openFileDialog2.FileName);
                                                if(contents.Contains(function))                                              

        {                                File.WriteAllText(openFileDialog2.FileName, File.ReadAllText(openFileDialog2.FileName).Replace(signal, replace));
                                                MessageBox.Show("Done");
                                            }

And even this,

using (var reader = new StreamReader(openFileDialog2.FileName))
                                {
                                 string currentLine;
                           if ((currentLine = reader.ReadLine()) != null)
                                    {
                                   while (currentLine.Contains(function))
                                        {
                                                                                     File.WriteAllText(openFileDialog2.FileName, File.ReadAllText(openFileDialog2.FileName).Replace(signal, replace));
                                            MessageBox.Show("Done");
                                        }}}

But nothing seems to work. IF the code works without the errors, then the signals outside the function are also updated.

In your second example it seems that you replace after writing the new content, you could try this:

string contents = File.ReadAllText(openFileDialog2.FileName);
if (contents.Contains(function))
{
    File.WriteAllText(openFileDialog2.FileName, contents.Replace(signal, replace));
    MessageBox.Show("Done");
}

Here we replace before writing back the edited content we read

Your first attempt is pointless, working on a filename instead of file contents. Your second attempt literally says, replace all signals if the file contains the function--no hint of the complexity needed to limit the scope of the 'signal' search to the required function. Your third attempt tries to do things line by line which is on the right track but from there it's nonsense.

You need to write a parser to limit the scope of your search.

Open an input file, and an output file. Read one line at a time. I. If it contains a function with the required name, begin accumulating it into a new string--from there, count curly brace opens and close until the count goes back down to 0 (end of function). At that point, replace 'signal' with 'replace' and then write it to the output. II. If it did not contain a function, output it directly.

If the code is machine generated, you might could check for a curly brace on a new line with no indentation to end a function. If the code isn't machine generated, then you're going to have a headache parsing it and should find some library which can parse c# code for you after you get tired of fixing the 100th bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM