Hello!

Basically I need this program to go line by line down my text file and tell me if it sees "000-00-0000"......we use this as a dummy number otherwise the file wont process.

Right now the program will tell me it sees "000-00-0000" in the file but a few lines might actually be missing "000-00-0000"

Any suggestions on how to make it search line by line?

class Program
    {
        static void Main(string[] args)
        {

            string fName = @"C:\Users\cmoy\Desktop\WorkOrder_01102011.txt";

            StreamReader testTxt = new StreamReader(fName);

            string allRead = testTxt.ReadToEnd();

            testTxt.Close(); 

            string regMatch = "000-00-0000";
            
           

            if (Regex.IsMatch(allRead,regMatch))

            {

                Console.WriteLine("found\n");

 

            }

            else

           {

                Console.WriteLine("not found\n");

            }

            Console.ReadLine();
 

        }

    }

}

>C# search line by line text

Use System.IO.File.ReadAllLines method.

string []lines=System.IO.File.ReadAllLines("filename.txt");

foreach(string line in lines)
 {
    //
 }

Where do I specify what to search for?

Search code must be placed inside the for loop.

string []lines=System.IO.File.ReadAllLines("filename.txt");
string regMatch = "000-00-0000";
foreach(string line in lines)
 {
   if (Regex.IsMatch(line,regMatch))
    {
      Console.WriteLine("found\n");
     }
    else
     {
      Console.WriteLine("not found\n");
    }
 }

well then you can use if statements in the for loop..

foreach (string line in lines)
{
if (line == "text your looking for")
{
Console.WriteLine("YOU HAVE A MATCH");
}
else if
{
Console.WriteLine("YOU DONT HAVE A MATCH");
}


}

It is still doing what my original code does. If I have 30 lines that contain "000-000-0000" and one line that doesnt it will still tell me that it was found "000-000-0000"

Well is "000-000-0000" not what you are looking for?

explain what you mean please

Sorry I did not explain this to well. I'm checking to make sure that every line of my flat file has "000-000-0000" because sometimes people forget to enter that it errors out the batch job.

I would like the program to tell me if a line does not have "000-000-0000".
Sorry for the explanation

string fName = @"C:\Documents and Settings\andre clements\Desktop\NewTextDocument.txt";
            //your path above to your file
            TextReader testTxt = new StreamReader(fName);//<-----could just write the path
                                                        //of your file straight in here

            string read; //declare read here so it is in scope outside of do loop
            do  //this is your loop until read is null
            {
                
               read = testTxt.ReadLine();//read next line from your file
                
                if (read != "0")//if read is not equal to what you want so in your
                {               //case 000-000-0000 then do this
                    Console.WriteLine("You have made an error");
                    break;//break out of loop if made an error
                }
                else
                {
                    Console.WriteLine("Your input is ok");
                }

            } while (read != null);//end do loop

is that the kind of thing you are after?

Adapost gave you a great starting point, you only have to build it up a bit:

private void Method()
        {
            string flag = "000-00-0000";
            string[] lines = System.IO.File.ReadAllLines(@"C:\1\test6.txt");
            foreach (string line in lines)
            {
                if (line.Contains(flag))
                {
                    MessageBox.Show("The number was found.");
                    break;
                }
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.